jQuery:从下拉列表中选择值后提交表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3822495/
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
jQuery: submit form after a value is selected from dropdown
提问by Omu
I have this form in my HTML:
我的 HTML 中有这个表单:
<form action="/awe/ChangeTheme/Change" method="post">
<select id="themes" name="themes">
...
<option value="blitzer">blitzer</option>
</select>
<input type="submit" value="change" />
</form>
Anybody knows how to submit it when a value is selected in the 'themes' dropdown?
任何人都知道如何在“主题”下拉列表中选择一个值时提交它?
回答by RoToRa
The other solutions will submit allforms on the page, if there should be any. Better would be:
其他解决方案将提交页面上的所有表单(如果有)。更好的是:
$(function() {
$('#themes').change(function() {
this.form.submit();
});
});
回答by xPheRe
In case your html contains more than one form
如果您的 html 包含多个表单
$(function() {
$('#themes').on('change', function(e) {
$(this).closest('form')
.trigger('submit')
})
})
回答by Rocket Hazmat
$('#themes').change(function(){
$('form').submit();
});
回答by Aaron
I recommend using the longhand bind method because it has the same effect as the shorthand supplied by the other answers, but you can add additional events if need be without having to change your code.
我建议使用普通绑定方法,因为它与其他答案提供的速记具有相同的效果,但是如果需要,您可以添加其他事件而无需更改代码。
$("#themes").bind("change", function() {
$("form").trigger("submit");
});
回答by Darin Dimitrov
$(function() {
$('#themes').change(function() {
$('form').submit();
});
});