2017-01-17 14 views
0

こんにちは、次のスクリプトを使用してdivを開きます。ラジオボタンを使用して選択オプションでdivを開く方法

<script> 
    function showfield(name) { 
     if(name=='Others') { 
      document.getElementById('div1').innerHTML='<input type="text" name="other" />'; 
     } else { 
      document.getElementById('div1').innerHTML='hai'; 
     } 
    } 
</script> 

をマイHTMLプログラムは、次のように:

<form id='form-id'> 
    <input id='watch-me' name='test' type='radio' value="a" />Show Div 
    <br /> 
    <input name='test' type='radio' onClick="showfield(this.options[this.selectedIndex].value)" value="Others" /> 
    <br /> 
    <input name='test' type='radio' value="c" /> 
</form> 
<div id="div1"></div> 

答えて

0

を入力通話機能で間違い:

次のように私の要求

私のスクリプトに基づいてすべての結果を表示することはできませんよ

<input name='test' type='radio' onClick="showfield(this.value)" value="Others" /> 
0

これを試すことができます。

function showfield(name){ 
 
    if(name=='Others'){ 
 
    document.getElementById('div1').innerHTML='<input type="text" name="other" />'; 
 
    }else{ 
 
    document.getElementById('div1').innerHTML='hai'; 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form id='form-id'> 
 
    <input id='watch-me' name='test' type='radio' onClick="showfield(this.value);" value="a" />Show Div 
 
    <br /> 
 
    <input name='test' type='radio' onClick="showfield(this.value);" value="Others" /> 
 
    <br /> 
 
    <input name='test' type='radio' onClick="showfield(this.value);" value="c" /> 
 
</form> 
 

 
<div id="div1"></div>

0

onClickイベントは、それが終わる括弧とちょうどthis.valueを必要とし、オフになっていました。

<input name='test' type='radio' onClick="showfield(this.value)" value="Others" /> 

はここリファクタリングJavaScriptを使用して作業JSBinです:http://jsbin.com/mehulov/1/edit?html,js,output

+0

注各入力はあまりにもonClickイベントを持っている必要があることを、私はJSBinデモにこれを追加しました。 – leocreatini

関連する問題