javascript 提交表单以选择选项。onClick
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6131346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Submit Form for select option.onClick
提问by Emsu
How would I submit a form when the user clicks an option using jQuery? I found one similar question using the datepicker text input but I'm not that familiar with jQuery so I can't seem to convert it to work for a select item.
当用户使用 jQuery 单击选项时,我将如何提交表单?我使用 datepicker 文本输入发现了一个类似的问题,但我对 jQuery 不太熟悉,所以我似乎无法将其转换为适用于选择项。
回答by Matt Ball
On click
of an <option>
:
对click
的<option>
:
$('option').click(function ()
{
$(this).closest('form').submit();
});
On change
of a <select>
(this is probably the one you want):
上change
的<select>
(这可能是你想要的):
$('select').change(function ()
{
$(this).closest('form').submit();
});
As @Guffa commented:
正如@Guffa 评论的那样:
The
click
event on options doesn't work in all browsers. Safari for example does't trigger it.
click
选项上的事件不适用于所有浏览器。例如,Safari 不会触发它。
...so you definitely want the second one.
...所以你肯定想要第二个。
回答by Guffa
The click
event on options doesn't work in all browsers. Use the change
event of the select
element.
click
选项上的事件不适用于所有浏览器。使用元素的change
事件select
。
Example:
例子:
$('select').change(function(){
this.form.submit();
});
Note that the submit
event of the form is not triggered when you call the submit
method to post the form.
注意,submit
当您调用该submit
方法发布表单时,不会触发表单的事件。
回答by Igor T.
A small correction of these answers:
对这些答案的小修正:
$(document).ready(function() {
$('#some_link').click(function() {
$('#form_id').submit();
});
});
回答by james
I found the other answers helpful but to truly achieve what I needed of individual actions based on each option click this was exactly what I needed:
我发现其他答案很有帮助,但要根据每个选项真正实现我需要的单个操作,单击这正是我所需要的:
回答by Ibu
$('#option').click(function () {
// form validation and such then
$('form').submit();
})
This is the easiest way i can think of
这是我能想到的最简单的方法
UPDATE:
更新:
for specific form, you can use its name or id, it really depends on how your html looks like
对于特定的表单,您可以使用其名称或 ID,这实际上取决于您的 html 的外观
$('form[name="formnamehere"]').submit();
回答by PeeHaa
$('select').change(function() {
$('form').submit();
});