[英]clear radio buttons when click on text input
我有一組 4 個單選按鈕,后跟一個文本輸入,當用戶單擊文本輸入字段時,我試圖清除所有單選輸入。 到目前為止,這是我的代碼。
<input type="radio" name="radio"><label for="radio1">1</label>
<input type="radio" name="radio"><label for="radio2">2</label>
<input type="radio" name="radio"><label for="radio3">3</label>
<input type="radio" name="radio"><label for="radio4">4</label>
<input type="text" id="textInput">
<script>
$('#textinput').click(function () {
radio.checked = false;
});
</script>
<script>
$('#textInput').click(function () {
$('input[type=radio]').removeAttr("checked");
});
</script>
或者你可以試試 attr() 方法
$('#textInput').click(function () {
$('input[name="radio"]').attr('checked',false);
});
我認為修改腳本塊(不更改 html)的最佳方法是首先確保代碼在准備好的文檔上運行,並且您可能應該確保事件是焦點,而不是單擊,以防有人使用鍵盤或備用導航:
$(function() {
$('#textInput').focus(function () {
$('input[name=radio]').prop("checked", false);
});
});
盡管您可能更可能只想清除其他選擇,前提是它們實際上在該字段中輸入了一些數據,但您可能希望這樣做:
$(function() {
$('#textInput').on('input', function () {
if($(this).val().length > 0) {
$('input[name=radio]').prop("checked", false);
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.