简体   繁体   English

在多选中选择所选选项

[英]Picking selected options in a multiple select

I have a multiple DDL with items built dynamically with PHP and I want to store the selected options on page load, so as to be able to restore them later discarding the changes made. 我有多个DDL,其中包含使用PHP动态生成的项目,我想在页面加载时存储所选的选项,以便以后恢复它们时可以放弃所做的更改。

So, after building the DDL: 因此,在构建DDL之后:

var store = [];
ob = document.getElementById('getall_writers'); nr = ob.length;
for(var i=0; i < nr; i++) {
    if(ob.options[i].selected) { store[i] = i ;}
}

What is my mistake? 我怎么了 It always has store.length=2 . 它始终具有store.length=2

I just tested and modified it a little bit to store the value (not the id), but it looks good: http://jsfiddle.net/ts92oafp/1/ 我只是测试并对其进行了一点修改以存储值(而不是ID),但是看起来不错: http : //jsfiddle.net/ts92oafp/1/

var store = [];
var ob = document.getElementById('getall_writers');
var nr = ob.length;
for(var i=0; i < nr; i++) {
    var option = ob.options[i];
    if(option.selected) {
        store.push(option.value);
    }
}
console.log(store);

Isn't this what you want? 这不是你想要的吗?

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

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