选择其他单选按钮时清除文本框

问题描述 投票:0回答:2

我昨天开始为网页编写一些代码,但我无法使某些事情正常工作。

我有4个单选按钮,其中3个具有应用的值,并且其中一个允许用户在其旁边输入文本字段的自定义金额,例如]

[] = 3

[] = 11

[] = 32

[] = [_________] =输入自定义金额

[问题是,如果用户通过按单选按钮选择自定义金额并输入300,例如,此后决定选择他们仍可以提交的预定义金额11,这将输入两个值。

因此,如果选择了预定义的单选按钮,我要做的就是在下面编写一些代码以清除文本框。

我现在遇到的问题是,说页面加载了,用户立即想要输入自定义金额,十分之九,他们很可能会按下或在文本字段内单击,而不是使用旁边的单选按钮但是因为我在启动时禁用了它,所以我不知道解决此UX问题的最佳方法是什么。谁能提供帮助?这是我到目前为止的内容:

  <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"/>

$('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('');
   }
});
javascript jquery textbox radio-button
2个回答
1
投票

关于这个?

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

<label><input type="radio" name="am_payment" value="3"> <strong>64</strong></label>

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

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

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

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

0
投票

我想您想要的是当有人单击文本输入时广播事件必须被触发并且输入必须是焦点

function activate() {
  console.log('clicked');
  $('#other').prop('checked', true);
  $('#other').trigger('click');
  $('#theamount').focus();
}
$('input[name="am_payment"]').on('click', function() {
  console.log('changed');
  if ($(this).val() === '') {
    $('input[name="CP_otheramount"]').val('');
    $('#theamount').removeAttr("disabled");
  } else {
    $('#theamount').prop("disabled", "disabled");
    $('input[name="CP_otheramount"]').val('');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.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" id="other">
<label>Other</label>

<span onclick="activate();"><input type="text" name="CP_otheramount" value="" id="theamount" disabled="disabled"/></span>
© www.soinside.com 2019 - 2024. All rights reserved.