jquery validate form.submit() 行为奇怪

发布于 2024-11-08 13:21:33 字数 3954 浏览 0 评论 0原文

我正在使用 JQuery 的验证插件来验证表单。进行验证并触发submitHandler。但后来我得到这个错误(来自 IE8):

网页错误详细信息

用户代理:Mozilla/4.0(兼容; 微星8.0; Windows NT 5.1;三叉戟/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E) 时间戳:2011 年 5 月 20 日星期五 16:30:56 世界标准时间

消息:对象不支持此功能 属性或方法

它在 Firefox 中正常工作 - 完全没有错误。知道为什么 form.submit() 方法不起作用吗?

JQuery

$(document).ready(function() {
    $.validator.addClassRules('valcount',{digits: true, min: 1});

    //Fish Form
    $("form[name='fish']").validate({
        debug: true,
        ignore: ":hidden,[name='']", // do not validate form fields in invisible sections
        errorPlacement: function(error, element) {
            if ( element.is(":radio") ) {
                error.appendTo( element.parent().next().next() );
            }else if(element.is(":checkbox") && element.parent().is('li')){
                error.insertBefore(element.closest("ul.horiz"));
            }else{
                error.appendTo(element.parent());
            };
        },
        rules: {
            fish_name: {required:true, minlength:5},
            care_id: {required: true, digits: true, min: 1},
            strain_id: {required: true, digits: true, min: 1},
            transgene_id: {digits: true, min: 0}
        },
        // set this class to error-labels to indicate valid fields
        success: function(label) { // set   as text for IE
            label.html(" ").addClass("checked");
        },          
        submitHandler: function(form){
            if(confirm('Are you sure?'))form.submit(); //PROBLEM var has no such method
        }
    });

});// end $(document).ready(function(){

表单 HTML

<form action='http://example.com' method='post' name='fish' id='fish'>
        <input name='fish_id' type="hidden" value="39" >

        <div><label for='fish_name'>Fish Name</label><input name='fish_name' id='fish_name' type='text' value='110208'></div>
        <div><label for='fishnotes'>Notes</label><textarea name='notes' id=fishnotes></textarea></div>
        <div><label for='care_id'>Animal Care Protocol</label><select name='care_id' id='care_id'><option value='1' selected="selected">Breeding Colony</option>
</select></div>
        <div><label for='strain_id'>Strain</label><select name='strain_id' id='strain_id'><option value='1' selected="selected">AB wt</option>
<option value='2' >Tub wt</option>
<option value='14' >AB/Tub</option>
<option value='15' >AB/Tub deadhead</option>
<option value='16' >Casper</option>
</select></div>
        <div><label for='transgene_id'>Transgene</label><select name='transgene_id' id='transgene_id'><option value=0>None</options><option value='1' >Ztag</option>
<option value='2' >KDR-eGFP</option>
<option value='3' >BTIAR-eGFP</option>
<option value='4' >BTIA-eGFP</option>
<option value='5' >BTTP3.1-eGFP</option>
<option value='6' >BTTP6.2-eGFP</option>
<option value='7' >BTIAR-Luc</option>
<option value='8' >BTIA-Luc</option>
<option value='9' >BTTP3.1-Luc</option>
<option value='10' >BTTP6.2-Luc</option>
<option value='11' >Tg(hsp70tolgfp)v28</option>
<option value='16' >blither</option>
</select></div>

<div><input value='Edit' type='submit' ></div>
</form>

编辑:删除了提交名称和 ID,添加了有关 FF 的信息。尝试使用 $(form).submit() 而不是 form.submit() 。还是没有喜悦。

I am using JQuery's validate plugin to validate a form. Validation occurs and the submitHandler is triggered. But then I get this error (from IE8):

Webpage error details

User Agent: Mozilla/4.0 (compatible;
MSIE 8.0; Windows NT 5.1; Trident/4.0;
.NET CLR 1.1.4322; .NET CLR 2.0.50727;
.NET CLR 3.0.04506.30; .NET CLR
3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C;
.NET4.0E) Timestamp: Fri, 20 May 2011
16:30:56 UTC

Message: Object doesn't support this
property or method

It works as it should in Firefox- no errors at all. Any ideas why the form.submit() method doesn't work?

JQuery

$(document).ready(function() {
    $.validator.addClassRules('valcount',{digits: true, min: 1});

    //Fish Form
    $("form[name='fish']").validate({
        debug: true,
        ignore: ":hidden,[name='']", // do not validate form fields in invisible sections
        errorPlacement: function(error, element) {
            if ( element.is(":radio") ) {
                error.appendTo( element.parent().next().next() );
            }else if(element.is(":checkbox") && element.parent().is('li')){
                error.insertBefore(element.closest("ul.horiz"));
            }else{
                error.appendTo(element.parent());
            };
        },
        rules: {
            fish_name: {required:true, minlength:5},
            care_id: {required: true, digits: true, min: 1},
            strain_id: {required: true, digits: true, min: 1},
            transgene_id: {digits: true, min: 0}
        },
        // set this class to error-labels to indicate valid fields
        success: function(label) { // set   as text for IE
            label.html(" ").addClass("checked");
        },          
        submitHandler: function(form){
            if(confirm('Are you sure?'))form.submit(); //PROBLEM var has no such method
        }
    });

});// end $(document).ready(function(){

Form HTML

<form action='http://example.com' method='post' name='fish' id='fish'>
        <input name='fish_id' type="hidden" value="39" >

        <div><label for='fish_name'>Fish Name</label><input name='fish_name' id='fish_name' type='text' value='110208'></div>
        <div><label for='fishnotes'>Notes</label><textarea name='notes' id=fishnotes></textarea></div>
        <div><label for='care_id'>Animal Care Protocol</label><select name='care_id' id='care_id'><option value='1' selected="selected">Breeding Colony</option>
</select></div>
        <div><label for='strain_id'>Strain</label><select name='strain_id' id='strain_id'><option value='1' selected="selected">AB wt</option>
<option value='2' >Tub wt</option>
<option value='14' >AB/Tub</option>
<option value='15' >AB/Tub deadhead</option>
<option value='16' >Casper</option>
</select></div>
        <div><label for='transgene_id'>Transgene</label><select name='transgene_id' id='transgene_id'><option value=0>None</options><option value='1' >Ztag</option>
<option value='2' >KDR-eGFP</option>
<option value='3' >BTIAR-eGFP</option>
<option value='4' >BTIA-eGFP</option>
<option value='5' >BTTP3.1-eGFP</option>
<option value='6' >BTTP6.2-eGFP</option>
<option value='7' >BTIAR-Luc</option>
<option value='8' >BTIA-Luc</option>
<option value='9' >BTTP3.1-Luc</option>
<option value='10' >BTTP6.2-Luc</option>
<option value='11' >Tg(hsp70tolgfp)v28</option>
<option value='16' >blither</option>
</select></div>

<div><input value='Edit' type='submit' ></div>
</form>

Edited: removed submit name and id, added info about FF. Tried with $(form).submit() instead of form.submit(). Still no joy.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

朦胧时间 2024-11-15 13:21:33
<input name='submit' id='submitfish' value='Edit' type='submit' >

您不能命名提交按钮“提交”。当您执行此操作并在 MSIE 中调用 form.submit() 时,MSIE 认为您正在尝试引用“不支持此属性或方法”的输入对象。

请记住,在 jQuery 中,$(this) 是一个 jQuery 对象,this 是实际的 DOM 元素。所以

submitHandler: function(form){

提交处理程序将表单作为 DOM 元素传递,这就是 form.submit() 在这里失败的原因。

<input name='submit' id='submitfish' value='Edit' type='submit' >

You can't name a submit button "submit". When you do this and call form.submit() in MSIE, MSIE thinks that you're trying to reference the input object, which "doesn't support this property or method."

Remember that in jQuery, $(this) is a jQuery object and this is the actual DOM element. So

submitHandler: function(form){

The submit handler is passing the form as a DOM element, which is why form.submit() fails here.

剩一世无双 2024-11-15 13:21:33

事实证明,问题是 jquery 1.6.x 和 validate 插件 1.8.x 之间的冲突。当我恢复到 jquery 1.5 时,问题就消失了。这不是一个很好的解决方案,但希望它可以让某些人免于一些麻烦。

预计到达时间:最近发布的 1.8.1 版验证似乎也将解决此问题。请注意,截至 5 月 24 日,1.8.0 是托管于 http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8/jquery.validate.min.js

It turns out the problem is a conflict between jquery 1.6.x and validate plug-in 1.8.x. When I reverted to jquery 1.5, the issue went away. It's not a very good fix, but hopefully it will save someone some head scratching.

ETA: it appears that validate version 1.8.1, recently released, will fix this problem too. Note that as of May 24th, 1.8.0 is the version hosted at http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8/jquery.validate.min.js

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文