jQuery点击事件问题 [英] jquery click event issues

查看:95
本文介绍了jQuery点击事件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Internet Explorer和Chrome下的.click()事件遇到了一些问题.

I'm having some issues with .click() event under Internet Explorer and Chrome.

所以,我有这个过滤器菜单:

So, I've got this menu of filters:

<div id="filter-checkboxes" style="text-align: left; margin-bottom: 20px;">
    <input type="checkbox" id="crop-checkbox" onclick="init_filter('crop');" />
    <span id="crop-span">Crop</span>
    <br />

    <input type="checkbox" id="resize-checkbox" onclick="init_filter('resize');" />
    <span id="resize-span">Resize</span>
    <br />

    [...]
</div>

init_filter(filter)最后调用另一个发送ajax请求的函数

The init_filter(filter) calls at the end another function that sends a ajax req

function apply(action)
{
    var apply_btn = $("#apply-filter-btn");
    var values = null;

    $(document).ready(function(){
        apply_btn.unbind("click");
        apply_btn.click(function(){
            switch (action)
            {
                case "crop":
                    values  =   "x=" + $("#x").val() + "&y=" + $("#y").val() +
                    "&w="   + $("#w").val() + "&h=" + $("#h").val();

                    if ($("#w").val() !== "0" && $("#h").val() !== "0")
                        apply_send_req(action, apply_btn, values);
                break;
            }
        });
    });
}

问题在于发送实际请求之前存在延迟. 这仅在Firefox中有效. 所以我要问的是如何防止这种情况发生?

The issue is that there is a delay before the actual request is sent. This works great only in Firefox... So what I'm asking is what can I do to prevent this ?

推荐答案

请记住,此代码都在函数内部,我认为您应该删除文档准备就绪部分.如果要在加载时调用应用",请将调用包装在文档ready语句中,而不是将其链接到函数中.这可能会解决您的问题,因为您可能在触发后将其添加到文档就绪事件中.

Bearing in mind this code is all inside a function, I think you should drop the document ready section. If you are calling "apply" on load, wrap the call up in the document ready statement, rather than chaining it inside a function. This may solve your problem as you may be adding to the document ready event AFTER it has fired.

function apply(action)
{
    var apply_btn = $("#apply-filter-btn");
    var values = null;

    apply_btn.unbind("click");
    apply_btn.click(function(){
        switch (action)
        {
            case "crop":
                values  =   "x=" + $("#x").val() + "&y=" + $("#y").val() +
                "&w="   + $("#w").val() + "&h=" + $("#h").val();

                if ($("#w").val() !== "0" && $("#h").val() !== "0")
                    apply_send_req(action, apply_btn, values);
            break;
        }
    });
}

这篇关于jQuery点击事件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆