簡體   English   中英

如何獲得dom修改選擇框的當前選擇選項?

[英]How to get the currently selected option of a dom modified select box?

我在腳本中使用了偽造的選擇框,這意味着原始選擇框已隱藏,並且出於樣式目的,正在顯示ul。 每當此列表的選定值更改時,原始選擇框也將更改。

有一個更改功能。 該函數中包含以下代碼:

var el = $(e.currentTarget) ;
$('#my_select_box').children().removeAttr('selected');
$('#my_select_box  option[value="'+el.attr('data-value')+'"]').attr('selected','selected');

更改之后,我想在另一個函數中檢索原始選擇框的新選擇值。

但是普通選擇器僅獲取原始值,而不獲取新值:

$('#my_select_box option:selected').val() //returns the wrong value

那么,如何在需要的函數中獲取新值呢?

我試過這個功能:

$("#my_select_box").bind("DOMSubtreeModified", function() {
    console.log($(this).find('option:selected').val());    
});

它返回正確的值,但不在我需要的地方。 那么如何從domtree live中檢索正確的選擇框值?

提前致謝!

而不是.attr('selected','selected'); ,嘗試.prop('selected',true);

暫無
暫無

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

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