【问题标题】:How to submit form if it has specific and optional text inside value?如果表单中包含特定和可选文本,如何提交?
【发布时间】:2015-12-05 09:25:08
【问题描述】:

根据 jquery 文档,我可以这样做:

$( "form" ).submit(function( event ) {
  if ( $( "input:first" ).val() == "correct") {
    $( "span" ).text( "Validated..." ).show();
    return;
  }

  $( "span" ).text( "Not valid!" ).show().fadeOut( 1000 );
  event.preventDefault();
});

但问题是: 如果 value 有可选的词来验证提交,例如,它也可以由“ok”、“good”组成。如何做到这一点?

【问题讨论】:

    标签: jquery input submit


    【解决方案1】:
    if ( ["correct", "ok", "good", "whatever"].indexOf($( "input:first" ).val().toLowerCase())!= -1) {
        $( "span" ).text( "Validated..." ).show();
        return;
      }
    

    对所有可能的值创建一个array,然后检查该值是否存在于Array 中。我添加了一个额外的.toLowerCase 以确保OK == ok

    【讨论】:

    • 完美!它甚至可能是最好的方式!谢谢!
    【解决方案2】:

    也许我误解了你,但是怎么样

    if( $.inArray( $( "input:first" ).val(), ['correct', 'ok', 'good'] ) )
    

    【讨论】:

      【解决方案3】:

      var txt = $( "input:first" ).val(); if (txt.indexOf( "yes")>=0 ||txt.indexOf( "ok") >=0){显示已验证...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-21
        • 1970-01-01
        • 2015-04-30
        • 1970-01-01
        • 2017-02-24
        • 1970-01-01
        • 2017-04-27
        • 1970-01-01
        相关资源
        最近更新 更多