简体   繁体   中英

How can I add element to an array calling a function with the onchange event?

I have a drop down list box and onchange I'm calling a function which should push this value to an array. The array keeps returning only one value.

Here is the js function:

function multSubType(sel){
    var objSel = document.getElementById('subType'+sel);
    var valSel = new Array();
    valSel.push(objSel.value);

}

Everytime the function executes, you're creating the array all over again. Move the variable declaration and array assignment outside the scope of the function:

var valSel = new Array();

function multSubType(sel) {
    var objSel = document.getElementById('subType' + sel);
    valSel.push(objSel.value);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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