Clearing a textbox field when a radio button is se

2019-09-13 02:45发布

I have a web form which allows users to donate money using the predefined radio buttons with a value assigned to them (all different numbers). I also have a choose your own amount textfield which they can write in a custom amount they wish to donate. I want to clear the custom textfield if a user selects a predefined choice.

So far I have created this:

HTML:

<input type="radio" name="am_payment" value="3" checked="checked"> <strong>64</strong>
<input type="radio" name="am_payment" value="11" checked="checked"> <strong>100</strong>
<input type="radio" name="am_payment" value="32" checked="checked"> <strong>250</strong>
<input type="radio" value="" name="am_payment"><label>Other</label>
<input type="text" name="CP_otheramount" value="" id="theamount" disabled="disabled"/>

JAVASCRIPT:

$('input[name="am_payment"]').on('click', function() {
   if ($(this).val() === '') {
      $('#theamount').removeProp("disabled");
   }
   else {
      $('#theamount').prop("disabled", "disabled");
      $('input[name="CP_otheramount"]').val("");
   }
});

But basing it on a value === true for value="" just doesn't seem right.

Is there a way to improve this?

Thanks

2条回答
神经病院院长
2楼-- · 2019-09-13 03:18

To disable/enable an element you need to set the value of the disabled property to true/false, removing the property doesn't work so you need

$('input[name="am_payment"]').on('click', function() {
  if ($(this).val() === '') {
    $('#theamount').prop('disabled', false);
  } else {
    $('#theamount').prop("disabled", true).val('');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="am_payment" value="3" checked="checked"> <strong>64</strong>

<input type="radio" name="am_payment" value="11" checked="checked"> <strong>100</strong>

<input type="radio" name="am_payment" value="32" checked="checked"> <strong>250</strong>

<input type="radio" value="" name="am_payment">
<label>Other</label>

<input type="text" name="CP_otheramount" value="" id="theamount" disabled="disabled" />

查看更多
Viruses.
3楼-- · 2019-09-13 03:18

Use removeAttr instead of removeProp and I have modified your code to accept amounts on textbox based on radio button selected

$('input[name="am_payment"]').on('click', function() {
   if ($(this).val() === '') {
     $('input[name="CP_otheramount"]').val('');
      $('#theamount').removeAttr("disabled");
   }
   else {
      $('#theamount').prop("disabled", "disabled");
      $('input[name="CP_otheramount"]').val($(this).val());
   }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="am_payment" value="3" checked="checked"> <strong>64</strong>
<input type="radio" name="am_payment" value="11" checked="checked"> <strong>100</strong>
<input type="radio" name="am_payment" value="32" checked="checked"> <strong>250</strong>
<input type="radio" value="" name="am_payment"><label>Other</label>
<input type="text" name="CP_otheramount" value="" id="theamount" disabled="disabled"/>

Update

From your comments I hope you are expecting below functionality.

$('#theamount').on('focus', function() {
  $("input:radio").prop("checked",false);
  $("input[value='']").prop("checked",true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="am_payment" value="3" checked="checked"> <strong>64</strong>
<input type="radio" name="am_payment" value="11" checked="checked"> <strong>100</strong>
<input type="radio" name="am_payment" value="32" checked="checked"> <strong>250</strong>
<input type="radio" value="" name="am_payment"><label>Other</label>
<input type="text" name="CP_otheramount" value="" id="theamount" disabled="disabled"/>

查看更多
登录 后发表回答