简体   繁体   English

HTML当select标签具有尺寸属性时,选择滚动条上的更改事件触发点击Chrome

[英]HTML Select change event fires on scroll bar click in Chrome when select tag has a size attribute

I have a simple select box with a size attribute and I want to call a function when its value changes. 我有一个带有size属性的简单选择框,我希望在值改变时调用一个函数。
So I add an onchange event to the select tag: 所以我在select标签中添加了一个onchange事件:

<select onchange="alert(this.options[this.selectedIndex].value);" size="6" id="selecttest">
  <option value = "1">1</option>
  <option value = "2" selected>2</option>
  <option value = "3">3</option>
  <option value = "4">4</option>
  <option value = "5">5</option>
  <option value = "6">6</option>
  <option value = "7">7</option>
  <option value = "8">8</option>
  <option value = "9">9</option>
  <option value = "10">10</option>
  <option value = "11">11</option>
  <option value = "12">12</option>
</select>

See http://jsfiddle.net/MGtJZ/2/ . http://jsfiddle.net/MGtJZ/2/

In Chrome [Version 27.0.1453.94 m] in Windows 7 Pro (not in IE or Firefox from my tests), the onchange event is fired when you simply click in the select box's scroll bar, without the value having changed. 在Windows 7 Pro中的Chrome [版本27.0.1453.94 m](我的测试中不在IE或Firefox中),只需单击选择框的滚动条,而不更改值,就会触发onchange事件。

This also happens if I have a jQuery change event registered instead of using pure JavaScript ( http://jsfiddle.net/MGtJZ/1/ ), ie, I remove the onchange attribute and register the change event handler like so: 如果我注册了jQuery更改事件而不是使用纯JavaScript( http://jsfiddle.net/MGtJZ/1/ ),也就是说,我删除了onchange属性并注册了更改事件处理程序,就会发生这种情况:

$(function () {
  $('#selecttest').change(function () {
    alert($(this).val());
  });
});

Is this what I should be expecting? 这是我应该期待的吗?

Note: This only happens if you click the scroll bar or arrows first. 注意:仅当您首先单击滚动条或箭头时才会发生这种情况。 If you click the selected value or another value, then click the scroll bar/arrows, this behavior stops. 如果单击所选值或其他值,然后单击滚动条/箭头,此行为将停止。

On my Chromium install it doesn't happen. 在我的Chromium安装上它不会发生。 I'm pretty sure this is a bug though and shouldn't be expected. 我很确定这是一个错误但不应该被预料到。 You could make a workaround pretty easy though: 你可以很容易地解决这个问题:

$(function () {
    var lastVal;
    $('#selecttest').change(function () {
        var v = $(this).val();
        if (v != lastVal) {
            fireYourRealOnChangeEventHere();

            lastVal = v;
        }
    });
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM