获取输入值并显示div

时间:2015-02-05 03:42:41

标签: javascript jquery

如何显示由输入值定义的div

例如,如果我在输入字段dress中输入#showme,则会显示。

<form>
  <input type="text" id="desc" value="" onKeyUp="myFunction()"/>
  <div id="showme">
    <input type="text" value="" />
    <input type="text" value="" />
  </div>

我试过这个,但遗憾的是没有工作

<script language="javascript" type="text/javascript">
  function myFunction()
    {
        var input = document.getElementById('desc')
        var div = document.getElementById('showme');
        div.innerHTML = div.innerHTML + input.value;
    }
</script>

2 个答案:

答案 0 :(得分:1)

myFunction方法上,选择showme div后,您可以通过将可见性设置为可见来使其可见:

    div.style.visibility = "visible";

JSFiddle Demo

答案 1 :(得分:1)

您可以执行类似

的操作

&#13;
&#13;
function myFunction(el) {
  var div = document.getElementById('showme');
  var descval = document.getElementById('descval');
  descval.innerHTML = el.value;
  div.style.display = el.value == 'dress' ? 'block' : 'none';
}
&#13;
#showme {
  display: none;
}
&#13;
<form>
  <input type="text" id="desc" value="" onKeyUp="myFunction(this)" />
  <div id="showme">
    <input type="text" value="" />
    <input type="text" value="" />
    <!-- a span is used here so that there wouldn't be multiple appends -->
    <span id="descval"></span>
  </div>
</form>
&#13;
&#13;
&#13;