如何在jquery ajaxform插件中使用$(this) [英] how to use $(this) in jquery ajaxform plugin

查看:238
本文介绍了如何在jquery ajaxform插件中使用$(this)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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?

推荐答案

假设你正在使用这个jQuery Ajax表单插件,成功方法的第四个参数将是jQuery包装的表单采取行动:

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

所以这应该有效:

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

这篇关于如何在jquery ajaxform插件中使用$(this)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆