Javascript 带有收音机和文本选择的HTML表单输入

Javascript 带有收音机和文本选择的HTML表单输入,javascript,forms,Javascript,Forms,我有一个文本输入区域,以HTML格式连接到单选按钮,如下所示: <fieldset class="w100"> <div class="rowElem align-left"> <input type="radio" id="plan_height" name="plan_height" value="standard6'2&quot;" checke

我有一个文本输入区域,以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>


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

检查此项

对于跨浏览器支持,您必须像这样添加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';
}

您愿意使用jQuery吗?当然可以,但如果可能的话,我想让它更简单,只包含在onKeyup或onChange表单事件中。您能演示一下如何使用onBlur/onChange/onKeyup吗?另外,您能确认第一个无线电控制的ID和值吗?我尝试添加
document.form.other_text.checked=true但它不起作用我更新了表单。。。我现在只需要在用户输入文本/单击文本输入框时选择单选按钮。这在单击单选按钮时可以将焦点集中在文本字段上!!!但是-如果用户只是在文本框中单击,我需要它自动选择无线电按钮(几乎与您刚才给我的代码相反),这似乎没有任何作用..?我需要将JS放入我的JS文件中吗?或者只是表单中的代码?如果您有一个外部js文件…那么请将其包含在该文件中,或者将其包含在页面脚本标记部分。另外,为了保持标记的干净性和可管理性,最好不要将javascript内联到标记中。。。它在JSFIDLE中工作得很好,但在我的站点上不适合我。我把
var other\u radio=document.getElementById('other\u text'),other\u height=document.getElementById('other\u height');other_height.addEventListener('focus',function(){other_radio.checked=true;});other_radio.addEventListener('change',function(){other_height.focus();})位于我的文档底部,在结束正文标记之前,onFocus事件处于适当位置,当我单击文本字段时,它不会选中单选按钮。如果您在IE中查看它,那么它将不起作用,因为您必须更改事件名称,即focus将更改为onFocus,change将更改为onchange