【发布时间】:2012-03-05 17:25:15
【问题描述】:
我在 HTML 表单中的单选按钮上附加了一个文本输入区域,如下所示:
<fieldset class="w100">
<div class="rowElem align-left">
<input type="radio" id="plan_height" name="plan_height" value="standard6'2"" checked >
<label>Standard 6'2"</label>
</div>
<div class="rowElem align-left">
<input type="radio" id="other_text" name="plan_height" value="Other height" onclick="document.getElementById('other_height').focus();" >
<input type="text" id="other_height" name="plan_height" value="Enter custom height" onFocus="if(this.value=='Enter custom height') this.value='';" onBlur="if(this.value=='') this.value='Enter custom height';">
<label for="other_text">Other</label>
</div>
</fieldset>
如果用户选择“其他”的第二个单选选项,我希望文本框自动成为焦点,以便他们输入值。此外,如果用户单击文本框输入值,我希望自动为他们选择单选按钮。
我尝试在表单元素上使用 onBlur 或 onChange 或 onKeyup,但似乎无法正常工作。
【问题讨论】:
-
你愿意使用 jQuery 吗?
-
当然可以,但如果可能的话,我想让它更简单,只包含在 onKeyup 或 onChange 表单事件中。
-
您能展示一下您是如何尝试使用 onBlur/onChange/onKeyup 的吗?另外,你能确认一下第一个遥控器的ID和值吗?
-
我尝试在 onBlur 事件中添加
document.form.other_text.checked = true;,但没有成功 -
我更新了表单...我现在只需要在用户输入文本/单击文本输入框时选择单选按钮。
标签: javascript forms