【问题标题】:Clearing a textbox field when a radio button is selected in Javascript在Javascript中选择单选按钮时清除文本框字段
【发布时间】:2016-01-21 05:40:45
【问题描述】:

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

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

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 === truevalue="" 似乎并不正确。

有什么办法可以改善吗?

谢谢

【问题讨论】:

标签: javascript jquery html textbox


【解决方案1】:

要禁用/启用元素,您需要将 disabled 属性的值设置为 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" />

【讨论】:

  • 这行得通,问题是它迫使用户使用单选按钮来选择一个选项。如果用户也想按文本框输入内容怎么办?
【解决方案2】:

使用removeAttr 而不是removeProp,我已经修改了您的代码,以根据选择的radio 按钮接受文本框上的金额

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

更新

希望您的 cmets 期待以下功能。

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

【讨论】:

  • jQuery 规范建议使用 .prop("disabled", true/false) 而不是添加和删除属性或属性
  • 这很好用,不过我刚刚注意到一个用户体验问题。而不是在启动时禁用文本框,如果用户在文本框内按下它旁边的单选按钮会怎样?如果我要输入一个金额,但如果它的残疾人可能认为它不起作用,我通常会按文本框而不是单选按钮。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-22
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-11
相关资源
最近更新 更多