有没有更好的jQuery解决方案this.form.submit();? [英] Is there a better jQuery solution to this.form.submit();?

查看:301
本文介绍了有没有更好的jQuery解决方案this.form.submit();?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想触发当前元素所在表单的提交事件。我知道的一个方法有时是:

  this.form.submit(); 

我想知道是否有更好的解决方案,可能使用jQuery,因为我不是100 %sure方法适用于每个浏览器。



编辑:

我的情况如下:

 < form method =get> 
< p>< label>字段标签
< select onchange =this.form.submit();>
< option value =blah> Blah< / option>
....
< / select>< / label>
< / p>
< / form>

我希望能够通过更改<选择>



我正在寻找的解决方案适用于任何表单中的任何字段,而无需知道形成。 $('form:first') $('form')不起作用,因为表单可能是在页面上的第三个。此外,我已经在网站上使用jQuery,所以使用一点jQuery并不是什么大问题。



那么,有没有办法让jQuery检索表单输入/选择/文本区是在?

解决方案

我认为你正在寻找的东西是这样的:

  $(field).closest(form)。submit(); 

例如,要处理onchange事件,您可以使用以下代码:

  $(在此处选择您的字段).change(function(){
$(this).closest(form)。submit() ;
});

如果由于某种原因,您没有使用jQuery 1.3或更高版本,则可以调用父母而不是最接近


I want to trigger the submit event of the form the current element is in. A method I know works sometimes is:

this.form.submit();

I'm wondering if there is a better solution, possibly using jQuery, as I'm not 100% sure method works in every browser.

Edit:

The situation I have is, as follows:

<form method="get">
    <p><label>Field Label
        <select onchange="this.form.submit();">
            <option value="blah">Blah</option>
            ....
        </select></label>
    </p>
</form>

I want to be able to submit the form on change of the <select>.

What I'm looking for is a solution that works on any field within any form without knowing the id or name on the form. $('form:first') and $('form') won't work because the form could be the third on the page. Also, I am using jQuery on the site already, so using a bit of jQuery is not a big deal.

So, is there a way to have jQuery retrieve the form the input/select/textarea is in?

解决方案

I think what you are looking for is something like this:

$(field).closest("form").submit();

For example, to handle the onchange event, you would have this:

$(select your fields here).change(function() {
    $(this).closest("form").submit();
});

If, for some reason you aren't using jQuery 1.3 or above, you can call parents instead of closest.

这篇关于有没有更好的jQuery解决方案this.form.submit();?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆