2012-03-05 122 views
0

我必须附加到HTML表单中的单选按钮的文本输入区域如下所示:HTML表单输入 - 选择

<fieldset class="w100"> 
         <div class="rowElem align-left"> 
          <input type="radio" id="plan_height" name="plan_height" value="standard6'2&quot;" 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,但似乎无法让它工作。

+0

你开到使用jQuery ? – 2012-03-05 17:26:02

+0

当然,但如果可能的话,我想保持它更简单,只需包含在onKeyup或onChange表单事件中。 – adamdehaven 2012-03-05 17:26:53

+1

你能告诉你如何尝试使用onBlur/onChange/onKeyup吗?另外,您是否可以确认第一个无线电控制的ID和值? – 2012-03-05 17:27:06

回答

0

检查了这一点http://jsfiddle.net/tzj6Z/7/

对于跨浏览器支持,你必须添加broswer检测这样

if(navigator.userAgent.indexOf('Firefox')>=0) { // Firefox 
    focus_event = 'focus'; 
} else if (navigator.userAgent.indexOf('Safari')) { // Opera, Safari/Chrome 
    focus_event = 'DOMFocusIn'; 
} else { // IE 
    focus_event = 'onfocusin'; 
} 
+0

这似乎没有做任何事情..? – adamdehaven 2012-03-05 17:47:36

+0

我已更新链接...再次检查 – Abhidev 2012-03-05 17:49:04

+0

是否需要将JS放入我的JS文件中?或者只是表单中的代码? – adamdehaven 2012-03-05 17:50:11

2

您是否尝试过onclick事件:onclick="document.getElementById('other_height').focus();"

+0

当单击单选按钮时,它专注于文本字段!但 - 如果用户只是在文本框中单击,我需要它自动选择收音机buttong(几乎与刚刚给我的代码相反) – adamdehaven 2012-03-05 17:36:31