在选择任何其他单选按钮时清除文本框
Clearing a textbox when any other radio buttons are selected
我昨天开始为一个网页编写一些代码,但我在让一些事情工作时遇到了麻烦。
我有 4 个单选按钮,其中 3 个应用了值,还有一个允许用户输入自定义金额,旁边有一个文本字段,例如
[] = 3
[] = 11
[] = 32
[] = [__________] = 输入自定义金额
问题是,如果用户通过按下单选按钮选择自定义金额并输入 300 例如,然后决定选择他们仍然可以提交的预定义金额 11,这将输入两个值。
所以我所做的是在下面编写一些代码,以便在选择了预定义的单选按钮时清除文本框。
我现在遇到的问题是,假设页面加载并且用户立即想要输入自定义金额,10 次中有 9 次他们更有可能在文本字段中按下或单击,而不是使用旁边的单选按钮,但由于我在启动时禁用了它,我不知道解决此 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('');
}
});
关于这个?
$('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" />
我想你想要的是当有人点击文本输入时必须触发无线电事件,并且输入必须聚焦
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>
相关文章:
- 如何在OnPaste方法中清除文本区域
- 如何防止在进行选择时清除文本框
- 如何使用Javascript在按Backspace键时清除文本框
- 如何从文本区域输入中清除文本
- 想要清除文本框+聚焦文本框+阻止asp.net在同一按钮上回发
- javascript onclick radio清除文本框
- 清除文本区域会中断其他JS函数
- Javascript:如何清除文本框并恢复其值
- 如何重新启用禁用的下拉列表以及如何清除文本框数据
- 如何在删除/后退/清除文本框中的文本时清除 td 内容
- 按钮在 jsfiddle 中起作用,但不清除文本编辑器中的有序列表
- jQuery/Javascript:如何使用Javascript或jQuery清除文本框上的最近搜索
- 通过单击清除文本输入
- 清除文本框,但使用 Java 脚本给出错误
- 取消选中复选框时隐藏和清除文本框
- IE 不会清除文本区域值
- JavaScript 清除文本框 DOM 仅清除顶部框
- 清除文本框字段后再次加载选择框
- 清除文本框 脚本不起作用
- 角度.js - Google 自动完成事件触发时的清除文本框