选中文本框后激活单选按钮 [英] Activate radiobutton when text box is selected
问题描述
我正在尝试在正在处理的表单上设置一些Javascript表单验证.我已经完成了所有的验证工作,但是我要做的一件事是我似乎无法弄清楚,也无法在网上找到很多与此有关的文章.
I am trying to setup some Javascript form validation on a form that I am working on. I have all of the validation working, but one thing I have been tasked with doing I can't seem to figure out and can't find many articles online about it.
我面临的挑战是,如果用户要在文本框中输入其他"字段,我希望自动选中其他"单选按钮.这是我在那里的代码,显然对我不起作用.
The challenge that I have is that if a user wants to type in the "Other" field on a text box I want the "Other" radio button to automatically get checked. Here's the code I had in there that obviously isn't working for me.
if (f.OtherTextValue.value !== '')
{
document.getElementById("OtherTextRadio").checked = true;
return false;
}
所以我想我要做的是:如果OtherTextValue(我的文本框)不为空,则获取我的单选按钮的ID并选中/选中它.
So what I think I am trying to do is this: If the OtherTextValue (my text box) is not blank then go get the ID of my Radio button and make it checked/selected.
我在Javascript领域还算陌生,但是正在尝试学习.
I am still fairly new in the world of Javascript, but am trying to learn.
谢谢您的帮助.
推荐答案
将事件处理程序绑定到text
元素,然后根据是否有值来设置checkbox
的状态:>
Bind an event handler to the text
element, and then set the state of the checkbox
according to whether there is a value or not:
$('#OtherTextValue').on('keyup', function() {
$('#OtherTextRadio').prop('checked', !!this.value.length);
});
您可能要使用其他事件,因为这仅在释放键时触发. 查看文档
You may want to use additional events, as this only triggers when a key is released. Check the documentation
这篇关于选中文本框后激活单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!