Javascript / jQuery:在多个Select中设置值(选择) [英] Javascript/jQuery: Set Values (Selection) in a multiple Select

查看:777
本文介绍了Javascript / jQuery:在多个Select中设置值(选择)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个选择:

<select name='strings' id="strings" multiple style="width:100px;">
    <option value="Test">Test</option>
    <option value="Prof">Prof</option>
    <option value="Live">Live</option>
    <option value="Off">Off</option>
    <option value="On">On</option>
</select>

我从数据库加载数据。然后我有一个这样的字符串:

I load data from my database. Then I have a string like this:

var values="Test,Prof,Off";

如何在多重选择中设置此值?已经尝试更改数组中的字符串并将其作为值放在多个中,但不起作用......!
有人可以帮我吗?谢谢!!!

How can I set this Values in the multiple select? Already tried change the string in an array and put it as value in the multiple, but doesnt work...! Can someone help me with this? THANKS!!!

推荐答案

使用动态选择器中的值迭代循环,使用属性选择器。

Iterate through the loop using the value in a dynamic selector that utilizes the attribute selector.

var values="Test,Prof,Off";
$.each(values.split(","), function(i,e){
    $("#strings option[value='" + e + "']").prop("selected", true);
});

工作示例 http://jsfiddle.net/McddQ/1/

这篇关于Javascript / jQuery:在多个Select中设置值(选择)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆