jquery 如何根据另一台收音机选择并切换?

lbsnaicq  于 2023-10-17  发布在  jQuery
关注(0)|答案(2)|浏览(83)
<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">

我有这2套按钮,我想自动切换收音机。条件是我只能选择一台收音机。例如,当我选择“CRM_QFID_is_recur_radio”按钮时,应选择“CRM_QFID_4_payment_processor_id”。
当我切换到CRM_QFID_month_is_recur_radio时,应选择“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);
});
edqdpe6u

edqdpe6u1#

如果接受的答案做你需要的,那么这是更干燥
我给了电台一个类,并使用它来测试
让我知道下面的代码是否也适用于您

$(".toggle-radio").on('click, change', function() {
  const isMonth = $(this).is("#CRM_QFID_month_is_recur_radio");
  $('#CRM_QFID_4_payment_processor_id').prop('checked', !isMonth);
  $('#CRM_QFID_6_payment_processor_id').prop('checked', isMonth);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>

<div id="crm-radio-is_recur_radio-wrapper" class="crm-radio-wrapper crm-radio-wrapper-200px selected">
  <label><input class="toggle-radio" value="" type="radio" id="CRM_QFID_is_recur_radio" name="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">
  <label><input class="toggle-radio" value="month" type="radio" id="CRM_QFID_month_is_recur_radio" name="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">
qmb5sa22

qmb5sa222#

如果你有两个收音机:

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

尝试将其应用于您的情况:

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

相关问题