[英]Set the default value in dropdownlist using jQuery
我的下拉列表中有很多選項,例如:
<option value="1">it's me</option>
我需要選擇誰才有價值這是我的標簽內,而不是像屬性的選項1
。
我如何使用 jQuery 做到這一點?
如果您想為此使用 jQuery,請嘗試以下代碼。
$('select option[value="1"]').attr("selected",true);
更新:
根據 Vivek 的評論,正確指出史蒂文斯皮爾伯格希望通過其文本值選擇該選項。
下面是更新后的代碼。
$('select option:contains("it\'s me")').prop('selected',true);
您需要使用:contains(text)
選擇器通過包含文本進行查找。
此外,jQuery prop在獲取和設置屬性時為 Internet Explorer 提供了更好的支持。
你可以這樣做:
$('#myCombobox').val(1)
val() 應該處理這兩種情況
<option value="1">it's me</option>
$('select').val('1'); // selects "it's me"
$('select').val("it's me"); // also selects "it's me"
$('#userZipFiles option').prop('selected', function() {
return this.defaultSelected;
});
$("#dropdownList option[text='it\'s me']").attr("selected","selected");
jQuery("select#cboDays option[value='Wednesday']").attr("selected", "selected");
這工作正常:
$('#country').val($("#country option:contains('It\'s Me')").val());
一行 jQuery 就搞定了這一切!
$("#myCombobox option[text='it\'s me']").attr("selected","selected");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.