2009-09-02 52 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);