在jQuery中,如果我将class=auto_submit_form
分配给表单,则只要更改任何元素,它将使用以下代码提交:
/* automatically submit if any element in the form changes */
$(function() {
$(".auto_submit_form").change(function() {
this.submit();
});
});
但是,如果我要仅在更改指定元素时才提交表单:
/* submit if elements of class=auto_submit_item in the form changes */
$(function() {
$(".auto_submit_item").change(function() {
$(this).parents().filter("form").submit();
});
});
我只是在学习jQuery。有一个更好的方法吗?
最佳答案
/* submit if elements of class=auto_submit_item in the form changes */
$(function() {
$(".auto_submit_item").change(function() {
$("form").submit();
});
});
假设页面上只有一个表单。如果不是,则需要使用
$(this).parents("form").submit()
选择当前元素的祖先形式