2009-09-02 39 views
1
<script type="text/javascript"> 
    function myfunction() { 
     if (document.getElementById('myform').value == "yes") { 
      document.getElementById('yesinput').style.display = ''; 
     } else { 
      document.getElementById('yesinput').style.display = 'none'; 
     } 
    } 
</script> 

<select name="myform" id="myform" onchange="myfunction()"> 
<option selected="selected">please select</option> 
    <option value="yes">Yes</option> 
    <option value="no">No</option> 
</select> 

<div id="yesinput" style="display:none"> 
<input name="input" type="text" /> 
</div> 

<div id="noinput" style="display:none"> 
<input name="input" type="text" /> 
</div> 

幫助你。如何選擇將在id =「noinput」下方有另一個輸入字段。Javascript - getElementById並幫助我

現在它的工作,如果我們選擇是的。讓我知道

+0

我不知道你問。 – Zoidberg 2009-09-02 18:47:52

+0

大聲笑..對不起,我..堅持要形容更多。 (大多數語言)也許有人應該運行代碼 – wow 2009-09-02 18:52:30

回答

4
function myfunction() { 
    if (document.getElementById('myform').selectedIndex == 0) { 
     document.getElementById('noinput').style.display = 'none'; 
     document.getElementById('yesinput').style.display = 'inline'; 
    } else { 
     document.getElementById('noinput').style.display = 'inline'; 
     document.getElementById('yesinput').style.display = 'none'; 
    } 
} 
+0

謝謝凱爾rozendo;) – wow 2009-09-02 18:58:04

+1

當然,很高興它幫助;) – 2009-09-02 19:28:37

0

您可以構建一個新的元素,並通過Javascript將它追加到DOM。舉個例子:

var newInput = document.createElement("input"); 
newInput.setAttribute("id", "newElement"); 
newInput.setAttribute("type", "text"); 
document.body.appendChild(newInput);