我刚刚在 Google Chrome 中的一个简单复选框的表单验证中遇到了问题:
$("#index_form").submit(function(event) {
var device = $("select[name=device]").val();
var agree = $("input[name=agree]").checked;
if (device==0) {
$("#device").html(" <b style='color:red'>You must enter a device!</b>");
event.preventDefault();
}
if (!agree) {
alert("You must agree to the terms and conditions!");
event.preventDefault();
}
});
在 Chrome 中,无论是否选中,alert
消息都会持续显示。在 Safari 中,没问题。我重置了 Chrome,甚至在两台不同的计算机上尝试过,问题仍然存在。该复选框位于 table
内,该表include
包含在 php
函数中。所有其他验证代码(包括 select
元素)都运行良好。我可以继续工作的唯一方法是禁用导致问题的代码行。
最佳答案
checked
是一种 DOM 元素属性,而不是 jQuery 属性。因此请改用:
var agree = $("input[name=agree]").prop('checked');
关于jquery - 使用 Jquery 进行表单验证不适用于 Chrome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27108889/