javascript - 在 Javascript 中选择单选按钮时清除文本框字段

标签 javascript jquery html textbox

我有一个网络表单,允许用户使用预定义的单选按钮捐款,并为其分配一个值(所有不同的数字)。我还有一个选择您自己的金额文本字段,他们可以在其中写入他们希望捐赠的自定义金额。如果用户选择预定义的选项,我想清除自定义文本字段。

到目前为止我已经创建了这个:

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

但是将其基于 value=""value === true 似乎并不正确。

有没有办法改善这个问题?

谢谢

最佳答案

要禁用/启用某个元素,您需要将禁用属性的值设置为 true/false,删除该属性不起作用,因此您需要

$('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" />

关于javascript - 在 Javascript 中选择单选按钮时清除文本框字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34916173/

相关文章:

javascript - Bootstrap : hide drop down menu when mobile and link to parent

javascript - 这些功能有什么区别?

javascript - 在 jQuery 中检测 "runs"的 DOM 元素

c# - Html 表,其中每一行都有一个复选框,想要将所有复选框值发送到 ajax 调用

html - 如何创建一个带有一个固定高度的顶部 div 和一个剩余高度的可以溢出的中间 div 的垂直 flexbox?

javascript - 如何使用 javascript(没有 JQuery)进行日期屏蔽?

javascript - flexslider 2 事件缩略图

javascript - 过滤数组的结果

javascript - 使 Leaflet 侧边栏 div 在标记单击时处于事件状态,反之亦然

javascript - 背景图像不透明度问题