jquery - 使用 jQuery 的 HTML 页面拒绝提交?

标签 jquery jquery-validate

我是 jQuery 的初学者,正在尝试构建一个简单的 html 页面。该页面使用 jQuery 渲染良好并正确验证/行为,但是当一切都有效时,单击“提交”按钮时不会提交该页面。我的html文件的完整页面代码如下。为什么表单拒绝提交?

<html>
<head>
<title>jQuery UI Example Page</title>
<link type="text/css" href="css/cupertino/jquery-ui-1.8.21.custom.css"   rel="stylesheet" />
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.21.custom.min.js"></script>
<script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>
<script type="text/javascript">
    jQuery.validator.setDefaults({
        debug: true,
        success: "valid"
    }); ;
</script>
<script>
    $.validator.addMethod(
    "regex",
    function (value, element, regexp) {
        var re = new RegExp(regexp);
        return this.optional(element) || re.test(value);
    },
    "Please check your input."
 );
</script>
<script type="text/javascript">
    /**
    * @author GeekTantra
    * @date 24 September 2009
    */
    /*
    * This functions checks where an entered date is valid or not.
    * It also works for leap year feb 29ths.
    * @year: The Year entered in a date
    * @month: The Month entered in a date
    * @day: The Day entered in a date
    */
    function isValidDate(year, month, day) {
        var date = new Date(year, (month - 1), day);
        var DateYear = date.getFullYear();
        var DateMonth = date.getMonth();
        var DateDay = date.getDate();
        if (DateYear == year && DateMonth == (month - 1) && DateDay == day)
            return true;
        else
            return false;
    }
    /*
    * This function checks if there is at-least one element checked in a group of
    * check-boxes or radio buttons.   
    * @id: The ID of the check-box or radio-button group
    */
    function isChecked(id) {
        var ReturnVal = false;
        $("#" + id).find('input[type="radio"]').each(function () {
            if ($(this).is(":checked"))
                ReturnVal = true;
        });
        $("#" + id).find('input[type="checkbox"]').each(function () {
            if ($(this).is(":checked"))
                ReturnVal = true;
        });
        return ReturnVal;
    }
</script>
<script type="text/javascript">
    $.validator.addMethod(
  "totalValidationDate",
   function (value, element) {

       return isValidDate(parseInt(value.split('/')[2]),  parseInt(value.split('/')[0]), parseInt(value.split('/')[1])) || this.optional(element);
   },
  "Please enter a date in the format mm/dd/yyyy"
);
</script>
<script>
    $(document).ready(function () {
        $("#commentForm").validate({
            rules: {
                date1: {
                    required: true,
                    totalValidationDate: true
                },
                textBox1: {
                    regex: "^[a-zA-Z'.\\s]{1,40}$"
                }
            }
        });
    });
</script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#date1").datepicker();
    });
</script>
</head>
<body>
<form id="commentForm" method="get" action="http://www.yahoo.com">
 <input type="text" name="date1" id="date1" />
 <input type="text" name="textBox1" id="textBox1" />
 <input type="submit" value="Submit" id="btnSubmit"/>
</form>
</body>
</html>

最佳答案

好消息!我能够重现您的问题并修复它:

在默认声明 block 中,添加以下内容:

submitHandler: function(form) {
        form.submit();
}

所以它看起来像:

jQuery.validator.setDefaults({
    debug: true,
    success: "valid",
    submitHandler: function(form) {
        form.submit();
    }
});

我测试过,有效。不幸的是,我不知道为什么你需要这样做,但这是解决办法。

对于那些好奇的人来说,稍微更新一下,发生这种情况的原因是 this line in the plugin source 。这是一个内置函数,用于在使用调试时防止发生提交。

关于jquery - 使用 jQuery 的 HTML 页面拒绝提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11335992/

相关文章:

jquery - 如何使用 jquery 匹配标签后的文本?

php - 在 ajax 函数中隐藏 jquery 元素

javascript - 如何使用 javascript 从包含 html 代码的变量中获取选定的 div 部分?

jquery - csstoggleClass 无法正常工作

jQuery Validate,需要阻止免费电子邮件地址(例如 Gmail、Hotmail)

javascript - 单击 anchor 元素时触发事件

jquery - 变量作为字段名jquery验证规则

jQuery 验证器不验证 onkeyup

javascript - 为什么只验证第一个表单?

javascript - JQuery 验证按钮切换上的输入