javascript - 在 html 中,如何使用输入 html 代码更改选择选项菜单?

标签 javascript jquery html

我的代码在表单选择选项 html 代码中运行良好。 但表单输入菜单没有。 下面是我的代码。

<script>
   function showUser(str)
 {
 if (str=="")
 {
 document.getElementById("txtHint").innerHTML="";
 return;
 } 
 if (window.XMLHttpRequest)
 {// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
 }
 else
 {// code for IE6, IE5
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
 xmlhttp.onreadystatechange=function()
 {
 if (xmlhttp.readyState==4 && xmlhttp.status==200)
 {
 document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
  }
}
 xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>

上面说得好!但下面是我的代码。为什么这不起作用? 输入类型=“提交”也不能很好地工作。 我该如何克服这个问题?

 <script>
  same as above...
 </script>
 </head>
 <body>
 <form>
 <select name="users" onchange="showUser(this.value)">
  ## Here is wrong, input element.##
      <input type="text" name="users" onkeyup="showUser(this.value)>
 </select>
 </form>
 <br>
 <div id="txtHint"><b>Person info will be listed here.</b></div>

有什么问题吗?? 帮助我!

最佳答案

您缺少结束引用

你有

onkeyup="showUser(this.value)>

应该是:

onkeyup="showUser(this.value)">

您也不能嵌套 <input>里面 <select> 。看这个jsfiddle并注意 </select>为红色,因为它无效。 fiddle 中的第二个选择正确完成。

关于javascript - 在 html 中,如何使用输入 html 代码更改选择选项菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21202412/

相关文章:

javascript - 为什么本地存储在javascript之后最后保存

jquery - 用 CSS3 过渡替换 jQuery 幻灯片效果

html - 背景图像覆盖页面上的文本

javascript - 使用 jQuery filter() 获取所有匹配的 .val()

javascript - 如何对 Vue 组件的 'v-show' 属性进行单元测试

javascript - 使用带有进度条和附加字段的核心 JavaScript 进行 Ajax 文件上传

jquery - DataTable 单击时获取隐藏列值

jquery - 在鼠标悬停在位于该 div 中心的图像上时展开 div

linux - 在 MacBook 上通过 HTTP 连接到 Linux 上的 VirtualBox

javascript - 如何将 Javascript 变量传递到 HTML 文件中