简体   繁体   English

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

[英]Clearing a textbox when any other radio buttons are selected

I started working on some code for a webpage yesterday & i am having trouble getting a few things working.我昨天开始为一个网页编写一些代码,但我在处理一些事情时遇到了麻烦。

I have 4 radio buttons, 3 of which have values applied to them & one which allows the user to enter a custom amount with a textfield next to it eg我有 4 个单选按钮,其中 3 个应用了值,一个允许用户输入自定义金额,旁边有一个文本字段,例如

[] = 3 [] = 3

[] = 11 [] = 11

[] = 32 [] = 32

[] = [_________] = Enter Custom Amount [] = [_________] = 输入自定义金额

The problem is, if a user selects custom amount by pressing the radio button and enters 300 eg, then afterwards decides to choose a predefined amount of 11 they can still submit which will enter both values.问题是,如果用户通过按下单选按钮选择自定义数量并输入 300,例如,然后决定选择预定义的数量 11,他们仍然可以提交,这将输入两个值。

So what I done was write some of this code below to CLEAR the textbox if a predefined radio button is selected.所以我所做的是在下面编写一些代码来清除文本框,如果选择了预定义的单选按钮。

The problem I have now is that say the page loads and the user instantly wants to enter a custom amount, 9 times out of 10 they are more than likely to PRESS or click inside the textfield rather than use the radio button next to it but because I have disabled it on startup i don't know what the best way to resolve this UX problem.我现在遇到的问题是说页面加载并且用户立即想要输入自定义数量,10 次中有 9 次他们更有可能在文本字段内按下或单击而不是使用它旁边的单选按钮,但是因为我在启动时禁用了它,我不知道解决这个 UX 问题的最佳方法是什么。 Can anyone give help?任何人都可以提供帮助吗? Here's what I have so far:这是我到目前为止所拥有的:

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

About this?对这个?

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

I guess what you want is that when someone click on the text input the radio event has to be triggered and the input has to be on focus我猜你想要的是当有人点击文本输入时,必须触发单选事件并且输入必须是焦点

 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM