我正在制作一个小型网站,该网站内有一个 <select></select>
标签。我想在用户按下某个键时自动填充选择。
好吧?现在让我们解决这个问题,以上所有方法在 Chrome 中都可以正常工作,但在 IE 中却不起作用。有人可以告诉我一种让它在 IE 中运行的方法吗?
这就是我正在处理的内容:
// Action that gets triggered by textfield 2
function CheckTwo() {
if (event.keyCode == 13) {
AddToList();
document.getElementById("two").value = "";
GiveFocus("two");
}
}
function AddToList()
{
var box = document.getElementById("selectbox");
var newoption = document.createElement("option");
newoption.text = document.getElementById("two").value;
box.add(newoption);
}
// Create array of all the items of the <select></select>
function CalculateList()
{
var barcodes = [];
var options = document.getElementsByTagName("option");
for (var i = 0; i < options.lenght; i++)
{
barcodes.push(options[i].text);
}
var barcodestring = barcodes.join(",");
}
我认为 html 与此类问题无关,因为我相信 Internet Explorer 无法处理 javascript。 但这里是以防万一出现问题:
<tr>
<td>Bonregel:</td>
<td><input type="text" name="bonregel" id="two" onkeyup="CheckTwo()" /></td>
</tr>
<tr>
<td>Bonlijst:</td>
<td><select id="selectbox" multiple></select></td>
</tr>
<tr>
最佳答案
尝试稍微改变一下你的函数和调用。
function CheckTwo(event){
var tKey = (event.which) ? event.which : event.keyCode;
if (tKey == 13){
AddToList();
document.getElementById("two").value = "";
GiveFocus("two");
}
}
<input type = 'text' name = 'bonregel' id = 'two' onkeyup = 'CheckTwo(event)' />
关于javascript - 在 Chrome 中添加 <select> 中的项目可以,在 IE 中则不行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30803485/