开发者

Select option removal function

The following function is supposed to remove a selected value from the selectbox, and update a hidden field with all values.

I have two problems with it: Lets say I have the following options in the selectbox : 1071,1000,1052 After removing the first option (1071), the hidden field is getting a value of 1052,1000,1000,

if I remove the second option (1000) the hidden field is 1052,1052,1071 if I remove the third one (1052), I get an options[...].value is null or not an object

Can someone plz help me fix this ?

function removeOptions() {
    var selectbox = document.getElementById('zipcodes');
    var zipcodes = document.getElementById('zip');
    var tmpArray = [];
    var i;
    for (i = selectbox.options.length - 1; i >= 0; i--) {
        if (selectbox.options[i].selected){
            selectbox.remove(i);
        }
    开发者_开发问答    tmpArray.push(selectbox.options[i].value);
    }
    zipcodes.value = tmpArray.join(',');

}


Assuming you don't need the selected value in hidden value, place the portion to push in tmpArray in else part

    if (selectbox.options[i].selected){
                selectbox.remove(i);
    }else{
            tmpArray.push(selectbox.options[i].value);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜