【问题标题】:prevent form submission when all input text fields are empty but allow submission when any of them in not empty with jquery当所有输入文本字段为空时阻止表单提交,但当任何输入文本字段不为空时允许提交
【发布时间】:2015-06-04 22:01:44
【问题描述】:

场景是我有一个带有动态多文本字段的表单。

<form id="add" action="" method="POST">
  <input type="text" id="product-$id" name="quantity[]" class="quantity" />
</form>

根据条件,它会生成多个输入字段。看起来是这样的

<form id="add" action="" method="POST">
<input type="text" id="product-1" name="quantity[]" class="quantity" />
<input type="text" id="product-2" name="quantity[]" class="quantity" />
<input type="text" id="product-3" name="quantity[]" class="quantity" />
    </form>

现在,如果所有文本字段都为空,我想阻止我的表单提交。 但是,如果其中任何一个有价值,我将允许提交表单。

【问题讨论】:

标签: javascript jquery validation input laravel-4


【解决方案1】:

我希望以下代码能让您了解如何继续。

function SubmitForm(){

     //get all the dynamic textbox in the form
      var quantities =$('#add').find('.quantity');
      var hasValue = false;

      $.each(quantities, function(i, txtbox)
        {
           if ($.trim($(txtbox).val()) != '')
             {
               hasValue = true;
               return false; //break
               }
        });

        if (!hasValue)
          {
            return; //do not proceed further
            }

      //code here as atleast one textbox has a value

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 2017-11-18
    • 2023-03-10
    • 2017-07-27
    • 1970-01-01
    • 2013-07-25
    • 2017-02-08
    相关资源
    最近更新 更多