简体   繁体   English

如何通过填写文本字段来检查单选按钮?

[英]How can a radio button get checked by filling in a text field?

I have a HTML form, with radio buttons and a text field for some of them. 我有一个HTML表单,其中有一些单选按钮和一个文本字段。 When filling in a textfield, I want the corresponding radio button to be checked. 填写文本字段时,我希望选中相应的单选按钮。

I suppose it's possible with Javascript. 我想Java语言是可能的。 Can anyone help me out? 谁能帮我吗?

Thanks! 谢谢!

Here is how you can do it without jquery. 这是没有jQuery的方法。 This will also uncheck the radio button if the input is blank. 如果输入为空,这还将取消选中单选按钮。

 var inputs = document.querySelectorAll('input[type="text"]'); for (var i = 0; i < inputs.length; i++) { var input = inputs[i]; input.addEventListener('input', function() { if (this.value == "") { this.nextElementSibling.checked = false; } else { this.nextElementSibling.checked = true; } }); } 
 <input type="text"> <input type="radio"> <input type="text"> <input type="radio"> 

If you have the radio buttons before the text fields, just change nextElementSibling to previousElementSibling . 如果文本字段之前有单选按钮,则只需将nextElementSibling更改为previousElementSibling

Explanation 说明

document.querySelectorAll('input[type="text"]') will return an array of DOM elements that represent each of your input text fields. document.querySelectorAll('input[type="text"]')将返回代表每个输入文本字段的DOM元素数组。

Then, a for loop is used to loop through each text field. 然后,使用for循环遍历每个文本字段。

For each text field, an event listener is added that will call the function that is defined whenever the user changes the input in the text field. 对于每个文本字段,将添加一个事件侦听器,该事件侦听器将在用户更改文本字段中的input时调用定义的函数。 Don't use on change here, because that would only trigger the event when focus leaves the text field (in other words, when the user clicks outside the text field). 在此不要使用change ,因为这只会在焦点离开文本字段时(换句话说,当用户在文本字段之外单击时)触发事件。

In the event listener, we can reference the current text field with this . 在事件侦听器中,我们可以使用this引用当前文本字段。

We can get a reference to the radio button that is next to the current text field using this.nextElementSibling . 我们可以使用this.nextElementSibling对当前文本字段旁边的单选按钮的引用。 Attempting to use this.nextSibling will not work, as it will find a node that contains the actual text in your input field. 尝试使用this.nextSibling将不起作用,因为它将在输入字段中找到包含实际文本的节点。

The if statement checks if the current text field is blank. if语句检查当前文本字段是否为空白。 If it is blank it will set the checked attribute of the corresponding radio button to false , but if it is not blank, it will set checked to true . 如果是空白的,将所设置的checked相应的单选按钮的属性为false ,但如果它不为空,它将设置checkedtrue

not sure if you have jquery on the page but with jQuery you could use 不知道页面上是否有jquery,但是使用jQuery可以使用

$('#idOfTextbox').on('input propertychange paste', function() {
    $('#idOfCheckbox').attr('checked', true);
});

You can use .on('input' ... 您可以使用.on('input' ...

Please check out the snippet. 请查看代码段。

 $('.container').on('input' , 'input[type=text]',function(e){ var that = $(this); that.prev().prop({'checked':that.val().length>0}); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class=container> <div><input type=radio /><input type=text></div> <div><input type=radio /><input type=text></div> <div><input type=radio /><input type=text></div> <div><input type=radio /><input type=text></div> <div><input type=radio /><input type=text></div> </div> 

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

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