使用Jquery的表单验证不使用Chrome

使用Jquery的表单验证不使用Chrome,jquery,google-chrome,Jquery,Google Chrome,我刚刚遇到了一个问题,我在谷歌Chrome中对一个简单复选框的表单验证: $("#index_form").submit(function(event) { var device = $("select[name=device]").val(); var agree = $("input[name=agree]").checked; if (device==0) { $("#device"

我刚刚遇到了一个问题,我在谷歌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中,无论是否选中,警报消息都会持续显示。在狩猎中,天气很好。我重置了Chrome,甚至在两台不同的电脑上试用过,但问题仍然存在。该复选框位于php函数包含的表中。包括select元素在内的所有其他验证代码都运行良好。我可以继续工作的唯一方法是禁用导致问题的代码行。

选中的是DOM元素属性,而不是jQuery属性。因此,请改用:

var agree = $("input[name=agree]").prop('checked');

解决这个问题的一种方法是在JavaScript旁边使用HTML5 required属性。Chrome支持这一点,但它不会对不支持的旧浏览器造成任何问题。谢谢-刚刚玩了一个w3学校的例子。太棒了!工作是一种享受。我刚刚在代码中做了一个注释,提醒我应该开始区分JQuery和DOM!