简体   繁体   English

无法使用jQuery动态设置我的选择列表的选择选项

[英]Unable to dynamically set the selected option for my select list using jQuery

I have the following select list inside my web page:- 我的网页内有以下选择列表:-

<select id="OrderStatus_12192838383111121" title="Status Required Field" class="ms-RadioText">
<option>In Progress</option>
<option>Waiting Customer Approval</option>
<option>Reject</option>
</select>

now i want to select one of the above options based on the value from another field, so i tried the following:- 现在我想根据另一个字段的值选择上述选项之一,所以我尝试了以下操作:

var pmname = $('[id^="OrderProjectManagerStatus_"][id$="Display"]').attr("title");
alert(pmname);
$('select[id^="OrderStatus_"]').text() == pmname;

now the alert showed the correct value, which as "Reject", but this option did not get selected inside my select list using $('select[id^="OrderStatus_"]').text() == pmname; 现在,警报显示了正确的值,即“ Reject”,但没有使用$('select[id^="OrderStatus_"]').text() == pmname;在我的选择列表中选择此选项$('select[id^="OrderStatus_"]').text() == pmname; . so can anyone advice how i can dynamically set the selected option for my select list using jQuery ?? 所以任何人都可以建议我如何使用jQuery为我的选择列表动态设置所选选项?

Thanks 谢谢

For selects you'll have to get the option element and set selected attribute to true 对于选择,您将必须获取option元素并将selected属性设置为true

$('select[id^="OrderStatus_"] option').filter(function(index, item) { return $(item).text().trim() === pmname }).attr("selected", true);

And also, don't forget to have a value attribute assigned to option like so: <option value="Something">Something<option> 而且,不要忘记为option分配一个value属性,如下所示: <option value="Something">Something<option>

can look below may be help you understand 可以在下面看可能对您有所帮助

$( "[id^="OrderProjectManagerStatus_"] option:selected" ).each(function() {

   str += $( this ).text();
 });

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

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