我在jQuery中的選擇列表中遇到問題。 我做了一個「全選」選項,其值爲「-1」,當我點擊它時,所有選項都被標記爲選中。 但是,當我想取消選擇「全選」選項時,我無法弄清楚如何知道我是否已經取消選擇「全選」選項。jQuery:需要知道在更改事件時選擇了哪個選項
任何人都知道我該怎麼做?
HTML:
<select id="selectlist" name="selectlist" checkedAll="false">
<option id="all" value="-1">Choose all</option>
<option value="1" choice="true">Option 1</option>
<option value="2" choice="true">Option 2</option>
<option value="3" choice="true">Option 3</option>
</select>
的Javascript:
$("#selectlist").live("change", function (event) {
if ($(this).val() == "-1") {
var isChecked = $("#selectlist").attr("checkedAll");
if (isChecked == "false") {
$("#selectlist").attr("checkedAll", "true");
$("#selectlist option[choice='true']").each(function() {
$(this).attr("selected", "selected");
});
}
else {
$("#selectlist").attr("checkedAll", "false");
$("#selectlist option[choice='true']").each(function() {
$(this).removeAttr("selected");
});
}
}
});
這是確切的js代碼? – Nix 2012-04-02 13:12:37
我在編輯ID的名稱之前發現了一些錯誤,然後我將其發佈到此處以便於閱讀。已經修復了問題中的代碼部分! – bale3 2012-04-02 13:13:30
爲什麼不能只用Choose All作爲選項,而不是試圖標記其他選項? – Nix 2012-04-02 13:16:37