【问题标题】:jQuery Validation not running in IE (works in FF) [duplicate]jQuery Validation 未在 IE 中运行(在 FF 中有效)[重复]
【发布时间】:2025-12-20 04:45:11
【问题描述】:

我有一个需要验证的简单表单。我已经让它在 FF 中工作,但它在 IE 中的行为很奇怪。我正在使用 jQuery 1.6 和 jQuery Validate 1.8。

以下是完整的 html 代码。如您所见,我已经尝试更改 charset 属性,因为我读到有些人在 IE 上成功地做到了这一点。仍然没有运气。奇怪的是,当您在 IE 中运行它时,submitHandler 被触发(显示的警报显示表单已“提交”)。就好像 validate 函数根本不会触发,即使整个表单都是空白的。有人可以试试这个或提出解决方案吗?

<html>

<head>

<script src="jquery.min.js" type="text/javascript" charset="ISO-8859-1"></script>
<script src="jquery.validate.min.js" type="text/javascript" charset="ISO-8859-1"></script>

<script>
$(document).ready(function() {

    var prospectTxErrors = {
        prospectName : {
            required : "An entry is required in field Prospect.",
            rangelength : "Prospect Name must be between 2 and 45 characters."
            },
        groupSize : {
            required : "An entry is required in field Group Size.",
            digits : "Numeric data is required in field Group Size."
        },
        zipCode : {
            required : "An entry is required in field Zip Code.",
            digits : "Numeric data is required in field Zip Code.",
            rangelength : "Zip Code must be 5 digits."
        }, 
        sicCode : {
            required : "This is an invalid SIC code. Enter the SIC again or search for the SIC code.",
            rangelength : "This is an invalid SIC code. Enter the SIC again or search for the SIC code.",
            digits : "This is an invalid SIC code. Enter the SIC again or search for the SIC code."
        }, 
        agencyProducer : {
            required : "An entry is required in the Agent field."
        },
        phone : {
            required : "A 10 digit entry is required for Phone field."
        }   
    };

    $.validator.setDefaults({ 
        submitHandler : function() { alert("submitted!"); }
    });

    $("#prospectForm").validate({
        errorLabelContainer : $("#errorDiv ul"),
        wrapper : "li",
        debug : true,
        rules : {
            prospectName : {
                required : true,
                rangelength : [2, 45]
            },
            groupSize : {
                required : true,
                digits : true
            },
            zipCode : {
                required : true,
                digits : true,
                rangelength : [5, 5]    
            },
            sicCode : {
                required : true,
                rangelength : [4, 4],
                digits : true
            },
            agencyProducer : {
                required : true 
            },
            phone : {
                required : true,
                digits : true
            }
        },
        messages : prospectTxErrors
    });
});
</script>

<title>Test Prospect</title>
</head>

<body>

<div id="errorDiv"><ul id="errorList"></ul></div>

<form class="prospectForm" id="prospectForm" method="get" action="">
<label>Prospect Name</label>
<input type="text"
     name="prospectName" 
     id="prospectName"
     class=""/>
<br></br>
<label>Group Size</label>
<input type="text"
     name="groupSize" 
     id="groupSize"
     class=""/>
<br></br>
<label>Zip Code</label>
<input type="text"
     name="zipCode" 
     id="zipCode"
     maxLength="5"
     class=""/>
<br></br>
<label>SIC Code</label>
<input type="text"
     name="sicCode" 
     id="sicCode"
     maxLength="4"
     class=""/>
<br></br>
<label>Agency/Producer</label>
<input type="text"
     name="agencyProducer" 
     id="agencyProducer"
     class=""/>  
<br></br>
<label>Phone</label>
<input type="text"
     name="phone" 
     id="phone"
     class=""/>  
<input class="submit" type="submit" value="Submit"/>

</form>

</body>
</html>

【问题讨论】:

    标签: jquery internet-explorer jquery-validate


    【解决方案1】:

    无需更改整个 jquery 版本。只需将您的 jquery.validate 版本更新为 1.9...这是因为 jquery.validate 版本与 jquery 版本 > 1.6 不兼容。解决方案很简单,您还需要更新 jquery.validate 的版本。您可以从 Microsoft 的 CDN 中找到当前版本 1.9 或从 GitHub 上找到最新版本:

    Microsoft Ajax CDN:http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js
    GitHub Jquery 验证:https://github.com/jzaefferer/jquery-validation/downloads

    【讨论】:

      最近更新 更多