简体   繁体   English

未单击选项时在选择标记上触发更改事件

[英]Change event being fired on select tag when option isn't clicked

I have a change event bound to a select tag. 我有一个绑定到选择标签的更改事件。 The first option in the select tag is disabled because I am using it as the default text for the menu. select标记中的第一个选项被禁用,因为我将其用作菜单的默认文本。 If the user clicks on the select tag, hovers over an option without selecting it, and then clicks outside of the select menu twice the change event is being fired. 如果用户单击选择标记,则将鼠标悬停在选项上而不选择它,然后在选择菜单之外单击两次,触发更改事件。 How can I prevent this change event from being fired. 如何防止触发此更改事件。

The HTML HTML

<select id="select">
    <option disabled="disabled" selected="selected">Title</option>
    <optgroup label="things">
        <option>cars</option>
        <option>boats</option>
        <option>world</option>
    </optgroup>
</select>

The jQuery jQuery的

$('#hide').click(function(){
    $('#select').hide();
});
$('#show').click(function(){
    $('#select').show();
});
$('#select').change(function(){
    alert($(this).val());
});

The code is just a sample of what my problem is. 该代码只是我的问题的一个示例。 I added the buttons because in my real code I am hiding the select menu when the user clicks outside of it. 我添加了按钮,因为在我的真实代码中,当用户在菜单外单击时,我将隐藏选择菜单。

Here is a fiddle with the above code. 这是上面代码的小提琴

EDIT 编辑

After looking at comments it looks to be a firefox specific issue as it works fine in Internet Explorer, Chorme, and Safari. 查看评论后,它似乎是特定于Firefox的问题,因为它可以在Internet Explorer,Chorme和Safari中正常工作。

You could always associate some data on the select with the current value and validate that it actually changed. 您始终可以将所选内容上的某些数据与当前值相关联,并验证其实际更改。

$('#hide').click(function(){
    $('#select').hide();
});
$('#show').click(function(){
    $('#select').show();
});
$('#select').data('currVal', 'Title');
$('#select').change(function(){
    if($(this).val() != $(this).data('currVal') {
        // this is actual value change
        alert($(this).val());
        $(this).data('currVal', $(this).val());
    }
});

I have updated the original fiddle with solution: 我已经用解决方案更新了原始的小提琴:

http://jsfiddle.net/TPpD6/4/ http://jsfiddle.net/TPpD6/4/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未单击时是否/其他语句更改单选选项的图像 - If/Else Statement Change Image of a Radio Option when it isn't Clicked 添加选项进行选择时会触发“更改” - “Change” is being fired when adding options to select 使用标记选项时,Chrome中不会触发浏览器通知onclick事件 - Browser notification onclick event doesn't get fired in chrome when tag option is used 对于select,如果对话框被转义或单击,则会触发一次更改事件 - Change event is fired once for a select if dialog is escaped or clicked out side 使用on(&#39;click&#39;)函数无法识别按钮标签是否被单击? - Button tag isn't being recognized as being clicked, with on('click') function? 如何在触发选项更改事件时自动关闭移动选择框? - how to automatically close mobile select box when an option change event is fired? 在 Edge 中触发单击文件输入时不会触发更改事件 - Change event isn't fired when triggering click on file input in Edge 未触发文件更改事件 - File change event not being fired jQuery触发时,不会触发通过addEventListener()添加的事件 - Event added with addEventListener() isn't fired when triggered by jQuery 鼠标传递选项时选择标签事件/ onmouseover选择选项 - select tag event when mouse pass on option / select option with onmouseover
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM