簡體   English   中英

選擇任何選擇框選項時(未更改)的jQuery回調

[英]Jquery callback when any select box option is chosen (not changed)

我有一個下拉選擇表單元素。 選擇一個選項后,我想做某事。 即使從下拉列表中選擇的值是已經選擇的選項。 因此,在這種情況下,“按更改”將不起作用。

我怎樣才能做到這一點?

由於您的選項“實際上”沒有更改,因此jQuery change事件不會自動觸發。 相反,您需要將其綁定。 使用@epoch建議的click事件:-

示例代碼:-

             //Intialize a local variable
             var x = 0;
             $('#single').click(function () {
                 //Increment local variable by 1.
                 x = x + 1;
                 //Since click event will be fired twice (once during focus & second while   selecting the option) compare '2' for same selection
                 if (x == 2) {
                     $(this).change();
                     x = 0;
                 }
             }).change(function () {
                 console.log(1);
                 //To reset the value of x back to 0, set it as -1 here (since click event will be fired after change).
                 x = -1;
             });

暫無
暫無

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

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