简体   繁体   English

jQuery单击功能在IE中不起作用

[英]jQuery click function not working in IE

I have an issue with a jQuery script I wrote that is driving me a bit nuts. 我有一个问题,我写的jQuery脚本让我有点疯狂。 As usual it works perfectly in Firefox but falls over in all IE versions. 像往常一样,它在Firefox中完美运行但在所有IE版本中都有所体现。

I have the following HTML. 我有以下HTML。 It is generated using Velocity so it should be part of the DOM: 它是使用Velocity生成的,因此它应该是DOM的一部分:

<select name="groups" id="groups" type="select">
<option value="group1">group1</option>
<option value="group2">group2</option>
<option value="group3">group3</option>
</select>

My jQuery script looks like this: 我的jQuery脚本如下所示:

jQuery(document).ready(function() {
    jQuery("#groups option").click(function () {
        alert(group);
        var group = jQuery(this).attr("value");
        AJS.safe.ajax({
            url: "/plugins/userlookup/userlookup.action",
            type: "POST",
            dataType: "json",
            data: {
                group: group
            },
            success: function(data) {
                alert(data);
                jQuery("#users").empty();
                for(var i=0; i < data.userList.length; i++) {
                    var userstr = "<option value=\""+data.userList[i]+"\">"+data.userList[i]+"</option>";
                    jQuery("#users").append(userstr);
                }
            }
        });
    });
});

Basically there is a drop down list of "groups" and upon selecting a group an AJAX call is made to fetch users from that group and populate another field. 基本上有一个“组”的下拉列表,并且在选择组时,进行AJAX调用以从该组中获取用户并填充另一个字段。 In IE, selecting groups does nothing, nor does it give any Javascript errors. 在IE中,选择组不会做任何事情,也不会给出任何Javascript错误。 I have tried to step through it but looks like the "click" function is not binding at all to any of the elements as none of those alerts are called and breakpoints are never hit in the debugger. 我试图通过它,但看起来“click”函数根本没有绑定到任何元素,因为没有调用这些警报,并且调试器中永远不会遇到断点。 I've tried calling the AJAX URL directly in the browser, but unlike Firefox I don't get the JSON output, IE tries the download the action and fails. 我尝试直接在浏览器中调用AJAX URL,但与Firefox不同,我没有得到JSON输出,IE尝试下载操作并失败。

I have stepped through all the code in Firefox and the backend code and that is all working. 我已经介绍了Firefox和后端代码中的所有代码,这一切都正常。

Can anyone see what could be wrong with what I've done? 任何人都可以看到我所做的事情可能出错吗?

Try using the change event instead of the click event. 尝试使用change事件而不是click事件。 That is, when the selection is changed, then update your user select with the related data. 也就是说,更改选择后,请使用相关数据更新用户选择。 Note, also, the change to get the selected option from the groups select ( this will be the select instead of the option). 另请注意,从组中选择获取所选选项的更改( this将是选择而不是选项)。

jQuery(document).ready(function() {
    jQuery("#groups").change(function () {
        var group = jQuery(this).find(':selected').attr('value');
        AJS.safe.ajax({
                url: "/plugins/userlookup/userlookup.action",
                type: "POST",
                dataType: "json",
                data: {
                        group: group
                },
                success: function(data) {
                        alert(data);
                        jQuery("#users").empty();
                        for(var i=0; i < data.userList.length; i++) {
                                var userstr = "<option value=\""+data.userList[i]+"\">"+data.userList[i]+"</option>";
                                jQuery("#users").append(userstr);
                        }
                }
        });
    });
});

I believe that the click event isn't actually generated in IE for the option elements, it's generated on the select itself. 我认为在IE中实际上并没有为选项元素生成click事件,它是在select本身上生成的。 This may be different, though, if you are using a multiselect, though it doesn't appear that you are. 但是,如果你使用的是多选,这可能会有所不同,尽管你看起来并不像。

Incidentally, in addition to using the change event... if a username may have a & or < character in, you've got an error and potential cross-site-scripting security hole, from where you've stuck the strings together with HTML without proper escaping. 顺便提一下,除了使用change事件之外......如果用户名可能包含&<字符,那么您就会遇到错误和潜在的跨站点脚本安全漏洞,从那里您将字符串放在一起HTML没有适当的转义。

The string-based method is also rather inefficient, as jQuery has to create and discard a select wrapper every time you call it. 基于字符串的方法也是相当低效的,因为jQuery每次调用它时都必须创建并丢弃一个select包装器。 Avoid creating dynamic HTML with content from strings; 避免使用字符串中的内容创建动态HTML; use DOM methods as well as jQuery ones where appropriate. 在适当的地方使用DOM方法和jQuery方法。 For example: 例如:

var options= $('#users')[0].options;
options.length= 0;
for(var i= 0; i<data.userList.length; i++)
    options[i]= new Option(data.userList[i]);

...is shorter, faster, easier to read and safer than the string-hacking variant. ...比字符串黑客变体更短,更快,更容易阅读和更安全。

Also, as in my case, a possible cause is Internet Explorer Enhanced Security Configuration (ESC). 此外,在我的情况下,可能的原因是Internet Explorer增强安全配置(ESC)。 Which is turned on by default on a Windows 2008 Server. 默认情况下在Windows 2008 Server上打开。 To disable, see this . 要禁用,请看这个

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

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