如何减少重复的jQuery代码

时间:2019-10-11 21:46:18

标签: javascript jquery function

jQuery('#edit-field-number-of-beneficial-owner-und').change(function() {
  if (jQuery(this).val() == 0) {
    jQuery(this).parent().parent('#edit-field-number-of-beneficial-owner').siblings('#edit-field-beneficial-owner-one').nextUntil('.form-actions').find('input, select, textarea').val('');
    jQuery(this).parent().parent('#edit-field-number-of-beneficial-owner').siblings('#edit-field-beneficial-owner-one').nextUntil('.form-actions').find('input:checkbox').attr('checked', false);
    jQuery(this).parent().parent('#edit-field-number-of-beneficial-owner').siblings('#edit-field-beneficial-owner-one').nextUntil('.form-actions').find('select').trigger("chosen:updated");
  } else if (jQuery(this).val() == 1) {
    jQuery(this).parent().parent('#edit-field-number-of-beneficial-owner').siblings('#edit-field-beneficial-owner-two').nextUntil('.form-actions').find('input, select, textarea').val('');
    jQuery(this).parent().parent('#edit-field-number-of-beneficial-owner').siblings('#edit-field-beneficial-owner-two').nextUntil('.form-actions').find('input:checkbox').attr('checked', false);
    jQuery(this).parent().parent('#edit-field-number-of-beneficial-owner').siblings('#edit-field-beneficial-owner-two').nextUntil('.form-actions').find('select').trigger("chosen:updated");
  } else if (jQuery(this).val() == 2) {
    jQuery(this).parent().parent('#edit-field-number-of-beneficial-owner').siblings('#edit-field-beneficial-owner-three').nextUntil('.form-actions').find('input, select, textarea').val('');
    jQuery(this).parent().parent('#edit-field-number-of-beneficial-owner').siblings('#edit-field-beneficial-owner-three').nextUntil('.form-actions').find('input:checkbox').attr('checked', false);
    jQuery(this).parent().parent('#edit-field-number-of-beneficial-owner').siblings('#edit-field-beneficial-owner-three').nextUntil('.form-actions').find('select').trigger("chosen:updated");
  }
});

3 个答案:

答案 0 :(得分:1)

如前所述,简化代码的主要步骤是将中间步骤存储在变量中。我想在数据结构中添加封装条件检查:

jQuery('#edit-field-number-of-beneficial-owner-und').change(function() {
    const $this = jQuery(this);
    const labels = ['one', 'two', 'three'];
    const target = $this.parent()
        .parent('#edit-field-number-of-beneficial-owner')
        .siblings(`#edit-field-beneficial-owner-${labels[+$this.val()]}`)
        .nextUntil('.form-actions');

    target
        .find('input, select, textarea')
        .val('');

    target
        .find('input:checkbox')
        .attr('checked', false);

    target
        .find('select')
        .trigger('chosen:updated');
});

答案 1 :(得分:0)

使用局部变量作为您正在使用的所有事物的“基础”结果。然后,您可以创建一个函数来处理您反复键入的步骤。

jQuery('#edit-field-number-of-beneficial-owner-und').change(function() {
  let numOwner = jQuery(this).parent().parent('#edit-field-number-of-beneficial-owner');
  let handle = function(sibs) {
    sibs.nextUntil('.form-actions').find('input, select, textarea').val('');
    sibs.nextUntil('.form-actions').find('input:checkbox').attr('checked', false);
    sibs.nextUntil('.form-actions').find('select').trigger("chosen:updated");
  }

  if (jQuery(this).val() == 0) {
    handle(numOwner.siblings('#edit-field-beneficial-owner-one'))
  } else if (jQuery(this).val() == 1) {
    handle(numOwner.siblings('#edit-field-beneficial-owner-two'))
  } else if (jQuery(this).val() == 2) {
    handle(numOwner.siblings('#edit-field-beneficial-owner-three'))
  }
});

答案 2 :(得分:0)

有许多方法可以处理此问题。这是一个例子。

将值0-2映射到等效的字符串中。将值用作数组的索引以获取字符串(请注意允许您使用template literals的反引号)。将一长串的jQuery调用存储到一个变量中,并将该变量用于find()调用。

jQuery('#edit-field-number-of-beneficial-owner-und').change(function() {
  let ownerMap = ['one', 'two', 'three']
  let owner = jQuery(this).val()

  let formAction = jQuery(this).parent().parent('#edit-field-number-of-beneficial-owner').siblings(`#edit-field-beneficial-owner-${ownerMap[owner]}`).nextUntil('.form-actions')
  formAction.find('input, select, textarea').val('');
  formAction.find('input:checkbox').attr('checked', false)
  formAction.find('select').trigger("chosen:updated")
});