除非选中复选框,否则 Jquery 停止表单提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4795872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery stop form submit unless checkbox selected
提问by edwardmlyte
I've got this code, but it doesn't seem to return true. The alert always shows up. Any thoughts (without needing to create a var in which to store that a checkbox has been selected as that just seems a bit hacky).
我有这个代码,但它似乎没有返回 true。警报总是出现。任何想法(无需创建一个变量来存储已选择的复选框,因为这看起来有点笨拙)。
Thanks.
谢谢。
$("#form").submit(function(e) {
$('input[type=checkbox]').each(function () {
if(this.checked){
return true;
}
});
alert("Please select at least one to upgrade.");
return false;
});
回答by Josh
You don't need to do the loop to determine if a checkbox is checked. The :checked
selector filters out all that are not checked. Then you can use $.length
to get a count of the checked inputs.
您不需要执行循环来确定是否选中了复选框。在:checked
选择过滤掉不检查所有。然后您可以使用$.length
来获取已检查输入的计数。
$("#form").submit(function(e) {
if(!$('input[type=checkbox]:checked').length) {
alert("Please select at least one to upgrade.");
//stop the form from submitting
return false;
}
return true;
});
Additionally, using your approach, a small change should make this work
此外,使用你的方法,一个小的改变应该可以使这个工作
$("#form").submit(function(e) {
$('input[type=checkbox]').each(function () {
if($(this).is(':checked')){
return true;
}
});
alert("Please select at least one to upgrade.");
return false;
});
I think you may be simply missing the $()
around this
我想你可能只是想念$()
周围this
回答by eykanal
Just to point out, you're using the id reference #form
. Did you mean to just refer to all forms, or does the form have the id form
?
只是要指出,您正在使用 id reference #form
。你的意思是只引用所有表单,还是表单有 id form
?
回答by John K.
Example of checking the number of checboxes checked...
检查选中的复选框数量的示例...
<script>
function countChecked() {
var n = $("input:checked").length;
$("div").text(n + (n <= 1 ? " is" : " are") + " checked!");
}
countChecked();
$(":checkbox").click(countChecked);
</script>
Above "$("input:checked")" returns the array of checked items... You could check the "length" of this array for being > 0.
上面的 "$("input:checked")" 返回已检查项的数组...您可以检查此数组的“长度”是否大于 0。
回答by Selva Raj
Code for click and enter the submit button :
单击并输入提交按钮的代码:
$(":input").bind("click keypress", function(evt) {
var keyCode = evt.which || evt.keyCode;
var sender = evt.target;
if (keyCode == 13 || sender.type == "submit") {
evt.preventDefault();
// add your validations
}
// and submit the form
}
回答by shraddha Tripathi
var atleast = 1;
function validate() {
debugger;
var CHK = document.getElementById("<%=chk1");
var checkbox = document.getElementsByTagName("input");
var counter = 0;
for (var i = 0; i < checkbox.length; i++) {
if (checkbox[i].checked) {
counter++;
}
}
if (atleast > counter) {
alert("Please select atleast " + atleast + " employee ");
return false;
}
return true;
}
<button type="submit" name="Initiate" value="Initiate" on click="return validate()" id=" initiate"></button>