[英]How can I get jQuery to programmatically items in this select list using IE and FF consistently?
我有一些jQuery代碼,可以在選擇列表中選擇一個選項。 第一次選擇一個項目時一切正常,但是第二次通過IE和FF不會清除所選的項目。 Chrome始終可用。
我已經創建了一個JSFiddle ,上面有我所看到的演示。 我已經嘗試了$("testUpdate option:selected")
和$("testUpdate > option:selected")
作為選擇器,每個僅在第一次工作。 我嘗試根據此文檔將Multiple標記更改為僅多個。
這是HTML和失敗的jQuery。 我一直在使用jQuery 1.9.1,但是在JSFiddle上,我看到了與較新版本相同的結果。
//HTML
<select id="testUpdate" name="testUpdateName" multiple="multiple" size="5">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
//jQuery that fails the 2nd pass
$("#testUpdate option:selected").each(function () {
//This is not executed
$(this).removeAttr('selected');
});
//Alternate selector to force all options to always be checked
//HTML is correct after this, but visually the list is not selected
//I would also prefer not to do this because the actual list will be hundreds of items
$("#testUpdate option").each(function () {
$(this).removeAttr('selected');
});
selected
值不會添加到DOM元素中,因此不是該選項的屬性。 您應該使用$.prop()
:
$("#testUpdate option[value='" + this.getCurrentSelectionValue() + "']").prop('selected', 'selected');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.