【问题标题】:Validation not working in pagination验证在分页中不起作用
【发布时间】:2015-11-09 11:49:39
【问题描述】:

在这里,我有多个带有复选框的记录。 一旦我提交,它就会给出错误,因为我创建了“你必须选择至少一个复选框”。 当我选择“全选”复选框时,它将在分页中选中复选框的所有页面。 但是,当我取消选中特定页面的复选框并选中其他复选框时,在我提交时选择复选框也会给我一个错误。 那么在这个带有分页的复选框验证中我哪里出错了?

我的代码:

$('#processorder').click(function() {
      checked = $("input[type=checkbox]:checked").length;

      if(!checked) {
        alert("Please select an Order(s)..!!");
        return false;
      }

    });

});

<div style="text-align: center;margin-bottom: 10px;"><input type="submit" id="processorder" name="processorder" value="Process Order" class="submit-green"/></div>

【问题讨论】:

  • 您的代码工作正常。您的应用的其他部分似乎有问题

标签: javascript checkbox pagination


【解决方案1】:

尝试在 if 语句之前添加一个额外的检查,如下所示:

$('#processorder').click(function() {
  checked = $("input[type=checkbox]:checked").length > 0; //Extra check

  if(!checked) {
    alert("Please select an Order(s)..!!");
    return false;
  }

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="checkbox" name="chk[]" value="Apples" />
<input type="checkbox" name="chk[]" value="Bananas" />

<div style="text-align: center;margin-bottom: 10px;"><input type="submit" id="processorder" name="processorder" value="Process Order" class="submit-green"/></div>

试试下面的sn-p,希望对你有帮助!

/Zorken17

【讨论】:

  • 如果您认为这是一个好的答案,请将 i 标记为一个。 @Viral Bhoot
【解决方案2】:

如您所见,您有一行导致此错误。只需将其删除即可。

小提琴:http://jsfiddle.net/bxvfn652/

$('#processorder').click(function() {
      checked = $("input[type=checkbox]:checked").length;

      if(!checked) {
        alert("Please select an Order(s)..!!");
        return false;
      }

    });

}); /*this line breaks your code. */

【讨论】:

  • 代码在有或没有空行的情况下都有效。这对用户有什么帮助?
猜你喜欢
  • 2018-03-07
  • 1970-01-01
  • 2012-03-20
  • 2019-12-16
  • 2011-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-18
相关资源
最近更新 更多