2012-12-11 70 views
4

我試圖綁定一個事件,當用戶從<select> 不需要更改默認的項目。是否有可能綁定選擇選項上選擇而不是更改

$(select).change();只有在項目被更改時纔有效。

我想知道是否有某些作品可以通過選擇其中一個選項,即使它具有相同的默認選項。

$('#page_body').on('select', '.pop_pin_select', function() { 
}); 

這是我到目前爲止嘗試,但它不會工作。

+0

可能的重複:[HTML select元素onchange觸發已經選擇的選項](http://stackoverflow.com/questions/3354367/html-select-element-onchange-trigger-for-already-selected-option)。 – insertusernamehere

+0

您可能希望有一個空白選項來強制「更改」事件。選擇根本不會以你試圖讓它們工作的方式工作。 – meagar

+0

是的這是最糟糕的情況下,有一個原因,我正在嘗試這種方式 –

回答

1

有一種解決方法;使索引0處的默認選擇的選項,動態的和不手動選擇(即禁用)和監聽平變化爲正常,改變爲索引0。然後平變化時除外,設置.options[0]到選擇標籤並將選擇更改爲它。 Here is an example。當用戶相同的選擇,因爲該指數是不斷變化的,即使該值不是從例如

document.getElementById('sel') 
    .addEventListener('change', function() { 
     if (this.selectedIndex === 0) return; 
     this.options[0].value = this.value; 
     this.options[0].textContent = this.options[this.selectedIndex].textContent; 
     this.selectedIndex = 0; 
    }, false);​ 

平變化

相關的JavaScript將閃光。

+1

@downvoter,請解釋你的downvote在評論中,所以我可以迴應它。 –

+0

我認爲你可能會工作 –

相關問題