<div id="crm-radio-is_recur_radio-wrapper" class="crm-radio-wrapper crm-radio-wrapper-200px selected"><input class="" value="" type="radio" id="CRM_QFID_is_recur_radio" name="is_recur_radio"><label for="CRM_QFID_is_recur_radio">One-Time</label></div>

<input class="payment_processor_easy-pay crm-form-radio" value="4" type="radio" id="CRM_QFID_4_payment_processor_id" name="payment_processor_id">



<div id="crm-radio-is_recur_radio-wrapper" class="crm-radio-wrapper crm-radio-wrapper-200px"><input class="" value="month" type="radio" id="CRM_QFID_month_is_recur_radio" name="is_recur_radio"><label for="CRM_QFID_month_is_recur_radio">Monthly</label></div>

<input class="payment_processor_nach crm-form-radio" value="6" type="radio" id="CRM_QFID_6_payment_processor_id" name="payment_processor_id" checked="checked">

我有这两套按钮,我想自动切换 radio .条件是我只能 Select 1个无线电. 例如,当我 Select "CRM_QFID_IS_RECUR_RADIO"按钮时,应该 Select "CRM_QFID_4_Payment_Processor_id".

当我切换到CRM_QFID_MONTH_IS_RECUR_RADIO时,应 Select "CRM_QFID_6_Payment_Processor_id".

如何在jQuery中做到这一点?我试过以下几种方法,但不能切换:

  $('input[name=is_recur_radio]').change(function() {
  $("input[name=is_recur_radio]:checked").attr("id") == "CRM_QFID_is_recur_radio" ? $('input[name=payment_processor_id][value=6]').prop("disabled", false) : $('input[name=payment_processor_id][value=6]').prop("disabled", true);
  $("input[name=is_recur_radio]:checked").attr("id") == "CRM_QFID_month_is_recur_radio" ? $('input[name=payment_processor_id][value=4]').prop("disabled", true) : $('input[name=payment_processor_id][value=4]').prop("disabled", false);
  $('input[name=payment_processor_id][value=6]').prop('checked', false);
  $('input[name=payment_processor_id][value=4]').prop('checked', false);
});

推荐答案

如果您有两台 radio :

<input id="r1" type="radio">Radio 1<br>
<input id="r2" type="radio">Radio 2<br>

选中后,您可以使用以下代码做任何您想做的事情:

// If one is checked, uncheck the other.
$('#r1').change(function() {
  if ($(this).is(':checked')) {
    $('#r2').prop('checked', false);
  }
})

$('#r2').change(function() {
  if ($(this).is(':checked')) {
    $('#r1').prop('checked', false);
  }
})

try 将其应用于您的情况:

//  when I select "CRM_QFID_is_recur_radio" button, "CRM_QFID_4_payment_processor_id" should be selected.

$('#CRM_QFID_is_recur_radio').change(function() {
  if ($(this).is(':checked')) {
    $('#CRM_QFID_4_payment_processor_id').prop('checked', true);
    $('#CRM_QFID_6_payment_processor_id').prop('checked', false);
  }
})

$('#CRM_QFID_month_is_recur_radio').change(function() {
  if ($(this).is(':checked')) {
    $('#CRM_QFID_6_payment_processor_id').prop('checked', true);
    $('#CRM_QFID_4_payment_processor_id').prop('checked', false);
  }
})

Jquery相关问答推荐

点击和touch 事件之间的jQuery冲突解决方法

在 Mastodon 中将 jQuery 添加到 Rails 6

Jquery 表数据无法在 html 上正确显示

从文本区域获取值

javascript 捕获浏览器快捷方式 (ctrl+t/n/w)

哪个 JQuery Select 器会排除与给定 Select 器匹配的父项的项目?

如何将 JavaScript 对象编码为 JSON?

如何使用 jQuery 将表格的一行滚动到视图 (element.scrollintoView) 中?

使用 jQuery click 处理锚点 onClick()

如何在jQuery中 Select 具有特定ID的所有元素?

如何使div全屏?

在不使用提交按钮的情况下触发标准 HTML5 验证(表单)?

在 jQuery 中处理多个 ID

将 CSS 应用于 jQuery 对话框按钮

jQuery ajax (jsonp) 忽略超时并且不触发错误事件

如何在 JavaScript 中的对象数组中查找值?

使用 jQuery 比较两个 Javascript 对象数组

你会如何比较 jQuery 对象?

用于发布和获取的 Ajax 教程

jQuery .val 更改不会更改输入值