簡體   English   中英

無法使用jQuery動態設置我的選擇列表的選擇選項

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

我的網頁內有以下選擇列表:-

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

現在我想根據另一個字段的值選擇上述選項之一,所以我嘗試了以下操作:

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

現在,警報顯示了正確的值,即“ Reject”,但沒有使用$('select[id^="OrderStatus_"]').text() == pmname;在我的選擇列表中選擇此選項$('select[id^="OrderStatus_"]').text() == pmname; 所以任何人都可以建議我如何使用jQuery為我的選擇列表動態設置所選選項?

謝謝

對於選擇,您將必須獲取option元素並將selected屬性設置為true

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

而且,不要忘記為option分配一個value屬性,如下所示: <option value="Something">Something<option>

可以在下面看可能對您有所幫助

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM