在jQuery中,如果我分配class=auto_submit_form给一个表单,只要有任何元素被更改,它就会被提交,代码如下:
/* automatically submit if any element in the form changes */
$(function() {
$(".auto_submit_form").change(function() {
this.submit();
});
});
Run Code Online (Sandbox Code Playgroud)
但是,如果我想要仅在指定的元素更改时提交表单:
/* submit if elements of class=auto_submit_item in the form changes */
$(function() {
$(".auto_submit_item").change(function() {
$(this).parents().filter("form").submit();
});
});
Run Code Online (Sandbox Code Playgroud)
我只是在学习jQuery.有一个更好的方法吗?
tva*_*son 23
/* submit if elements of class=auto_submit_item in the form changes */
$(function() {
$(".auto_submit_item").change(function() {
$("form").submit();
});
});
Run Code Online (Sandbox Code Playgroud)
假设您在页面上只有一个表单.如果没有,您需要选择使用当前元素的祖先形式$(this).parents("form").submit()
Dar*_*o Z 11
您可以使用parents()方法中的表达式来过滤父项.因此,这可能会更有效:
/* submit if elements of class=auto_submit_item in the form changes */
$(".auto_submit_item").change(function() {
$(this).parents("form").submit();
});
Run Code Online (Sandbox Code Playgroud)
我会给表单一个id:
$(".auto-submit-item").change(function() {
$("form#auto-submit").submit();
});
Run Code Online (Sandbox Code Playgroud)