我有一个网络表单,允许用户使用预定义的单选按钮捐款,并为其分配一个值(所有不同的数字)。我还有一个选择您自己的金额文本字段,他们可以在其中写入他们希望捐赠的自定义金额。如果用户选择预定义的选项,我想清除自定义文本字段。
到目前为止我已经创建了这个:
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/