简体   繁体   English

取消常规点击的选择-IE不起作用

[英]Unselect on normal click - IE doesn't work

I have a bunch of elements that needs to be able to unselect on click (without CTRL+Click, because users are dumb and won't figure it out without intensive training, lol) 我有一堆元素需要能够在单击时取消选择(没有CTRL + Click,因为用户很笨,而且如果不进行深入培训就不会弄清楚,大声笑)

I have the following code, works brilliantly in Chrome/Firefox, not in IE 11 (don't have earlier versions to test on yet): 我有以下代码,在Chrome / Firefox(而不是IE 11)中运行出色(尚无要测试的早期版本):

$('[id^=sel_]').on('mousedown', 'option', function(e){
    e.preventDefault();

    if ($(this).is(':selected')) {
        $(this).prop("selected", false);
    } else {
        $(this).prop("selected", true);
    }

    $(this).change();

    return false;
});

As you can see it's pretty simple, everything with an ID that starts with "sel_" should get a "mousedown" event on their options. 如您所见,这很简单,所有以“ sel_”开头的ID的内容都应在其选项上出现“ mousedown”事件。 If the option is selected, unselect. 如果选择了该选项,请取消选择。 If the option is not selected, select. 如果未选择该选项,请选择。

I've tried "mouseup" and different iterations of the same function. 我已经尝试过“ mouseup”和同一功能的不同迭代。 IE entirely ignores it for some reason. IE出于某种原因完全忽略了它。 I've read elsewhere on Stack (for checkboxes) that it doesn't work in IE, but jQuery's documenation states it does work for all versions of IE that they support. 我已经在Stack的其他地方(针对复选框)阅读了它在IE中不起作用的信息,但是jQuery的文档说明它确实适用于它们支持的所有IE版本。

Any help would be highly appreciated. 任何帮助将不胜感激。

jsfiddle: http://jsfiddle.net/576A5/ jsfiddle: http : //jsfiddle.net/576A5/

I'm sorry but, the issue depends of specific implementation of select element native to each browser. 抱歉,问题取决于每个浏览器固有的select元素的具体实现。 IE handles it in a different way and it's making your workaround broke. IE以不同的方式处理它,这使您的解决方法崩溃了。

I think a solution can be to implement your own custom select control or use a ready plugin for that (like chosen ). 我认为解决方案可以是实现您自己的自定义select控件或为此使用现成的插件(如chosen )。

Using a stable plugin will help you to handle mobile devices/tablets, that with a custom control need particolar attention. 使用稳定的插件将帮助您处理需要自定义控件的移动设备/平板电脑。

1) Use $('[id^="sel_"]') 1)使用$('[id^="sel_"]')

2) Use .attr("selected", true); 2)使用.attr("selected", true); (instead of prop) (代替道具)

3) Ommit the 'option' parameter in the event binding 3)在事件绑定中省略'option'参数

Note: is the mouse event fired at all? 注意:是否完全触发了鼠标事件? (use console.log) (使用console.log)

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

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