[英]Is it possible to bind select option On select and not On change
i am trying to bind an event when user selected an item from <select>
not necessary changed from the default. 我正在尝试绑定一个事件,当用户从
<select>
选择一个项目时,没有必要从默认值更改。
$(select).change();
only works if the item was changed. 仅在项目更改时有效。
i wonder if there is something that works by selecting one of the options even if its the same default option. 我想知道是否有一些东西可以通过选择其中一个选项,即使它是相同的默认选项。
$('#page_body').on('select', '.pop_pin_select', function() {
});
This is what i try so far but it wont work. 这是我到目前为止尝试但它不会工作。
There is a way around this; 有一种解决方法; making the option at index 0 the default selected, dynamic and not manually selectable (ie disabled ) and listen for onchange as normal, except when changing to index 0. Then onchange , set
.options[0]
to chosen label and value and change selection to it. 使索引0处的选项成为默认选择,动态且不可手动选择(即禁用 )并正常监听onchange ,除非更改为索引0.然后onchange ,将
.options[0]
设置为所选标签和值并更改选择它。 Here is an example . 这是一个例子 。
Relevant JavaScript from example 来自示例的相关JavaScript
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);
onchange will fire when a user makes the same selection because the index is changing even though the value isn't. 当用户进行相同的选择时, onchange将触发,因为索引正在改变,即使值不是。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.