0

在 IE9 中出现错误 Microsoft JScript 运行时错误:'populatedropdown' is undefined on the line<input type="button"....

我假设我没有正确转义文字值。我应该如何改变?

<html>
<head>
<title>Add items to dropdown</title>

<script type="text/javascript">
function populatedropdown(var devicelist) {
    var devList = document.frm.optdevices;
    var arrDev = split(devicelist, ";");

     devList.options.length = 0; // this removes existing options

     for (var i = 0; i <= 3; i++) {
         var option = new Option(arrDev[i],i);
         devList.options[i] = option; 
     }
}

</script>

</head>
<body>
<form name="frm">

<select name="optdevices">
</select>

<input type="button" value="TestAdd" onclick="populatedropdown('201;202;203;')" />

</form>
</body>
</html>
4

3 回答 3

1

将 JavaScript 与 HTML 分离并删除语法错误。看到这个小提琴

function populatedropdown(devicelist) {
    var devList = document.frm.optdevices;
    var arrDev = devicelist.split(';'); // split not defined

     devList.options.length = 0; // this removes existing options

     for (var i = 0; i <= 3; i++) {
         var option = new Option(arrDev[i],i);
         devList.options[i] = option; 
     }
}

// on window load
window.addEventListener('load', function load(){
    // attach event listener to button
    document.getElementById('testID').addEventListener('click', function click(){
        populatedropdown('201;202;203;');
    }, false);
}, false);
​

HTML

<form name="frm">

<select name="optdevices">
</select>

<input id="testID" type="button" value="TestAdd" />

</form>​
于 2012-11-11T18:25:34.023 回答
0

您的函数无效,因为函数参数中有 var。只要您的脚本中有语法错误,该函数就不会被识别。function populatedropdown(var devicelist)无效。var参数中不应该有

function populatedropdown(devicelist) {
    var devList = document.frm.optdevices;
    var arrDev = split(devicelist, ";");

     devList.options.length = 0; // this removes existing options

     for (var i = 0; i <= 3; i++) {
         var option = new Option(arrDev[i],i);
         devList.options[i] = option; 
     }
}
于 2012-11-11T18:22:25.427 回答
0

首先,你不应该通过定义函数来编写var,第二次使用函数split是错误的,应该使用split var result = strToSplit.split(";"),如果你想分割;,你可以阅读关于split here Javascript Split method

这是工作片段

function populatedropdown(devicelist) {
    var devList = document.frm.optdevices;
    var arrDev = devicelist.split(";");

     devList.options.length = 0; // this removes existing options

     for (var i = 0; i <= 3; i++) {
         var option = new Option(arrDev[i],i);
         devList.options[i] = option; 
     }
}

刚刚在IE9上测试过

于 2012-11-11T18:25:49.507 回答