如何在jquery ajaxform插件中使用$(this)

我使用ajaxform在类似的字段集中有3个表单。 我想要的是当一个表单更新时,它应该只更新其父字段集。 现在发生的是因为我没有$(this)变量我不能指定ajaxform我只想更新提交的表单:

$(".toggle-form-submit").parents("form").ajaxForm({ dataType: 'html', success: function(html) { var myForm = $(this); console.log(myForm); if(myForm.parents("fieldset").find(".replaceable").length) { updateReplaceableAndClearAndCloseFormWithin(myForm.parents("fieldset"), html); } else { longPanelUpdateReplaceableAndClearAndCloseFormWithin(myForm.parents("fieldset"), html); } if( $(".test-categories-list").length) { initSortableTestCases(); } } }); 

显然myForm是响应对象。 我想要的是当前表单的jquery选择器,以便它可以找到它的父级。 我无法在ajaxform实例化中设置变量,所以我应该在哪里设置$(this)/ myForm?

假设您使用的是这个jQuery Ajax表单插件,成功方法的第四个参数将是被执行的jQuery包装表单:

http://jquery.malsup.com/form/#options-object

所以这应该工作:

 $(".toggle-form-submit").parents("form").ajaxForm({ dataType: 'html', success: function(html, status, xhr, myForm) { console.log(myForm); if(myForm.parents("fieldset").find(".replaceable").length) { updateReplaceableAndClearAndCloseFormWithin(myForm.parents("fieldset"), html); } else { longPanelUpdateReplaceableAndClearAndCloseFormWithin(myForm.parents("fieldset"), html); } if( $(".test-categories-list").length) { initSortableTestCases(); } } });