jquery - 使用 Jquery 进行表单验证不适用于 Chrome

标签 jquery google-chrome

我刚刚在 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/

相关文章:

javascript - jQuery:live() 与 delegate()

jquery - 我怎样才能防止用户在文本框中插入字符,而不是只有数字可以插入 jQuery Mobile

javascript - 文本框和选择框将值组合在单个文本中(第一个 tb 值,然后是 sb 值)

javascript - 单击 CSS/JQuery 动画 DIV

javascript - 谷歌浏览器 : Simultaneously 'smooth' scrollIntoView() with more elements doesn't work

Java Selenium Chrome 驱动程序 - 禁用图像加载

javascript - 重定向到数据 : 时 chrome.webRequest 跨源不允许方案

javascript - 将值与字符串分开

Google Chrome 版本的 Windows 注册表位置

css - 在 firefox 和 chrome 上打印 viewer bird 报告中的 pdf 文件