最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Setting the selected options in a multiselect programmatically - Stack Overflow

programmeradmin0浏览0评论

I have a multiselect on which I want to select some options on page load. The options will be selected using jQuery or javascript according to values of the options. Those values are stored in a variable as a string - exmaple below. What would be the logic to select those options?

var values = "1,3,5";

I have a multiselect on which I want to select some options on page load. The options will be selected using jQuery or javascript according to values of the options. Those values are stored in a variable as a string - exmaple below. What would be the logic to select those options?

var values = "1,3,5";
Share Improve this question edited Dec 8, 2011 at 9:10 Reporter 3,9485 gold badges35 silver badges49 bronze badges asked Dec 8, 2011 at 8:52 navnav 572 silver badges12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

here is the example code:

var items = "1,3,5";
$.each(items.split(','), function(idx, val) {
    $("select option[value='"+val+"']").attr("selected", "selected");
});

and here's a working example: http://jsfiddle/ftte4/

发布评论

评论列表(0)

  1. 暂无评论