【问题标题】:Separate children behaviour input on click jQuery单击 jQuery 时分离子行为输入
【发布时间】:2021-11-02 19:37:53
【问题描述】:

https://codepen.io/btek/pen/qBjaddP

我正在尝试在 select 上分离此选择,以便仅从一个产品中选择选项。

我正在尝试 each() , children()。

$('[name="subscribe"], [name="not_subscribe"]').off('click').on('click', function (event) {
    var isSubscribe = $(event.target).val() === 'subscribe';

    if ($(event.target).prop("checked")) {
      $('input[type="checkbox"][value="not_subscribe"]').prop('checked', !isSubscribe);
      $('input[type="checkbox"][value="subscribe"]').prop('checked', isSubscribe);
      $('[data-amrecurring="recurring-settings"]').toggle(isSubscribe);
    } else {
      $('input[type="checkbox"][value="not_subscribe"]').prop('checked', isSubscribe);
      $('input[type="checkbox"][value="subscribe"]').prop('checked', !isSubscribe);
      $('[data-amrecurring="recurring-settings"]').toggle(!isSubscribe);
    }



  }.bind(this));

【问题讨论】:

  • 不清楚您要做什么,但很明显您不需要 .off 和 .bind

标签: jquery input selection


【解决方案1】:

为了方便起见,我已将您的代码移至 JSFiddle。我只更改了javascript。

我将事件拆分为“取消订阅”和“订阅”,并通过两个函数找到了您需要的项目:parentsfind

$(event.target).parents('form').find('[name="not_subscribe"]').prop('checked', false);
$(event.target).parents('form').find('[data-amrecurring="recurring-settings"]').toggle(true);

toggle这里没有必要,你知道你想要的状态。使用不带参数的showhide

$(event.target).parents('form').find('[data-amrecurring="recurring-settings"]').show();

JSFiddle example

【讨论】:

    猜你喜欢
    • 2019-07-02
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 2018-01-01
    • 2015-05-27
    • 1970-01-01
    • 2015-02-11
    • 2023-03-06
    相关资源
    最近更新 更多