how to use $(this) in jquery ajaxform plugin

为君一笑 提交于 2020-03-03 10:09:26

问题


i have 3 forms in similar fieldsets using ajaxform. What i want is when a form is updated, it should only update its parent fieldset. what happens right now is because i don't have a $(this) variable i can't specify ajaxform that i only want to update the submitted form:

$(".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();
    }
  }
});

Apparently myForm is the response object. what i want is the jquery selector for the current form so that it can find it's parents. I can't set a variable in the ajaxform instantiation so where should i set $(this)/myForm?


回答1:


Assuming you are using this jQuery Ajax form plugin, the 4th argument of the success method will be the jQuery wrapped form that was acted on:

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

So this should work:

$(".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();
  }
 }
});


来源:https://stackoverflow.com/questions/5374800/how-to-use-this-in-jquery-ajaxform-plugin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!