我有一个选择,在单击时触发事件。但是当我点击相应的选项时,我想阻止它再次触发。
有办法吗?
答案 0 :(得分:2)
答案 1 :(得分:0)
只需使用onchange事件即可。以下是一个示例:https://jsfiddle.net/rafb0vyw/2/
$('#sel').on('change', function() {
alert('change');
})
答案 2 :(得分:0)
您可以检查event.target
属性,该属性具有不同的值。像这样:
var select = document.querySelector('select');
var span = document.querySelector('span');
select.onclick = function(e) {
// show e.target in snippet (you would remove this line in your code):
span.textContent = 'You clicked on ' + e.target.tagName + '.' +
' Current selected value is ' + select.value;
if(e.target.tagName !== 'SELECT') return; // ignore
// The rest of your event handling comes here...
// This will only be executed when you click the SELECT and not the OPTION.
};
<select>
<option value = 'A'>Option A</option>
<option value = 'B'>Option B</option>
</select>
<span></span>
确保在事件处理函数中定义了事件参数(在上例中名为 e )。