我的代码在表单选择选项 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/