jQuery验证没有在IE中运行(在FF中工作)

时间:2011-06-08 13:11:07

标签: jquery internet-explorer jquery-validate

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

以下是完整的HTML代码。正如你所看到的,我已经尝试更改charset属性,因为我读过一些人为IE做了这个成功。仍然没有运气。奇怪的是,当你在IE中运行它时,submitHandler被触发(显示警告表示表单已“提交”)。这就好像验证函数永远不会触发,即使整个表单都留空。有人可以试试这个或建议解决方案吗?

<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>

2 个答案:

答案 0 :(得分:2)

jQuery Validation not working in IE7 + IE8

请参阅此前回答的问题。同样的问题,也可能在这里工作。

答案 1 :(得分:0)

没有必要更改整个jquery版本..只需将你的jquery.validate版本更新为1.9 ...这是因为jquery.validate版本与jquery版本不兼容&gt; 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