Jquery:如果有给定的文本,请选择一个Select选项
我有一些预定义的值,我从Jquery:如果有给定的文本,请选择一个Select选项,jquery,performance,Jquery,Performance,我有一些预定义的值,我从 var str= $('#update-select').attr('data-type'); var substr = vals.split(','); //gives shipper, tester substr.forEach(function(item) { // select the option from select box which has this string // make selected = selected });
var str= $('#update-select').attr('data-type');
var substr = vals.split(','); //gives shipper, tester
substr.forEach(function(item) {
// select the option from select box which has this string
// make selected = selected
});
我有一个选择
<select multiple="" id="update-select" data-type="shipper, tester">
<option value="3">Shipper</option>
<option value="4">Created by</option>
<option value="5">test</option>
</select>
但是不起作用试试这个
$("#update-select option[text=" + item+ "]").attr('selected', 'selected');
使用
:包含选择器:
$('#update-select :contains(' + item + ')').prop('selected', true);
这里有一个演示:。那么你的问题是什么?
$('#update-select :contains(' + item + ')').prop('selected', true);