【问题标题】:Javascript - getElementById and help meJavascript - getElementById 并帮助我
【发布时间】:2010-11-25 01:29:53
【问题描述】:
<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>

帮助你。如果我们选择 No 将会在 id="noinput" 下面有另一个输入字段。

现在如果我们选择Yes,它就可以工作了。告诉我

【问题讨论】:

  • 我不知道你在问什么。
  • 大声笑.. 对此感到抱歉.. 我不得不描述更多。 (主要是语言)也许有人应该运行代码

标签: javascript onchange


【解决方案1】:
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';
    }
}

【讨论】:

  • 当然,很高兴它有帮助;)
【解决方案2】:

您可以构造一个新元素并通过 Javascript 将其附加到 DOM。一个例子:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 2011-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多