简体   繁体   English

将字符串文字从html传递到javascript函数

[英]Pass a string literal from html to a javascript function

In IE9 I am getting an error Microsoft JScript runtime error: 'populatedropdown' is undefined on the line <input type="button".... 在IE9中,我收到一个错误Microsoft JScript运行时错误: <input type="button"....行上未定义'populatedropdown'。

I assume I am not escaping the literal values correctly. 我假设我没有正确地转义文字值。 How should I change? 我应该如何改变?

<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>

Separate the JavaScript from HTML and remove syntax error. 将JavaScript与HTML分开,并删除语法错误。 See this fiddle 看到这个小提琴

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 的HTML

<form name="frm">

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

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

</form>​

Your function is not valid as you have var in the function parameter. 您的函数无效,因为在function参数中包含var。 As long as you have syntax error in your script, the function won't be recognized. 只要您的脚本中有语法错误,该函数就不会被识别。 function populatedropdown(var devicelist) is not valid. function populatedropdown(var devicelist)无效。 There should not be var in the parameter 参数中不应包含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; 
     }
}

First, you should not write var via defining function, and second use of function split is wrong, split should be used like var result = strToSplit.split(";") , if you wanna split by ; 首先,您不应该通过定义函数来编写var,其次,函数split的使用是错误的,如果您想通过;拆分,则应该像var result = strToSplit.split(";") split ; , you can read about split here Javascript Split method ,您可以在此处阅读有关split的内容Javascript Split方法

here is working snippet 这是工作片段

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; 
     }
}

just tested on IE9 刚刚在IE9上测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM