简体   繁体   English

jQuery .on()仅在第一次使用

[英]jQuery .on() works the first time only

First, I am admittedly new at jQuery and I know very well this isn't a new question. 首先,我当然是jQuery的新手,我知道这不是一个新问题。 Upon searching I see many similar posts like this, but I've tried the solutions from those posts and they bear no positive result. 搜索后,我看到了许多类似的帖子,但是我尝试了这些帖子中的解决方案,但没有正面的结果。 I've added a list of some of the things I've tried at the bottom. 我在底部添加了一些我尝试过的东西的列表。

How it works so far... 到目前为止如何运作...

I'm working on a single page application in ASP.NET and jQuery. 我正在ASP.NET和jQuery中开发单页应用程序。 I use web methods in my Default.aspx.cs to integrate with C# and my database. 我在Default.aspx.cs中使用Web方法与C#和数据库集成。

The application takes employees names from a database and creates a box on the screen for each employee using an ASP.NET repeater. 该应用程序从数据库中获取员工姓名,并使用ASP.NET转发器在屏幕上为每个员工创建一个框。 Those boxes can then be dragged and dropped into flyout containing jQuery droppables which will update the employees state in the database based on which box they drop in. This all works 100% fine. 然后可以将这些框拖放到包含jQuery droppable的弹出窗口中,这将根据员工插入的框来更新数据库中的员工状态。所有这些都可以100%正常工作。

The Problem... 问题...

I'm trying to add a cancellable timeout feature so a person can cancel their interaction before it commits to the database. 我正在尝试添加可取消的超时功能,以便一个人可以在提交数据库之前取消其交互。

Everything works fine for the first attempt to drop a box in a dropzone, by fine I mean 100% as expected including logging state changes to the database. 第一次尝试将一个盒子放到放置区中,一切都正常,我的意思是100%符合预期,包括将状态记录到数据库中。 Then for each drop after that only the timeout feature works, the okay and cancel buttons don't show up on the screen at all, yet after 3 seconds the timeout feature confirms the action and the desired state change is logged to the database. 然后,对于每次删除之后,只有超时功能起作用,“确定”和“取消”按钮根本不会显示在屏幕上,但是3秒钟后,超时功能确认了操作,并将所需的状态更改记录到了数据库中。

My jQuery script is fairly lengthy (like this post (sorry)) so I'm going to try and share only the most relevant portion of code, hopefully it will be enough for you to provide some insight. 我的jQuery脚本相当冗长(如这篇文章(对不起)),因此我将尝试仅共享最相关的代码部分,希望它足以为您提供一些见识。

default.aspx Default.aspx的

<div class="timeout">
    <div class="really">
        Confirm?
        <div class="progress">
            <div class="progress-bar" role="progressbar" data-transitiongoal="105"></div>
        </div>
    </div>
    <div class="okay">
        <img src="Content/ok3.png" />
    </div>
    <div class="cancel">
        <img src="Content/cancel3.png" />
    </div>
    <br />
</div>
<div class="promptpicker">
    <div>
        <h2>Select Date-Range</h2>
        <p>Select the first and last dates to enter your time off.</p>
    </div>
    <input id="e4" name="e4">
    <div class="submitDates">
        Submit
    </div>
</div>
...
//This code continues to show the asp.net repeaters for the employee boxes and flyout zones...

app.js (code in question) app.js (相关代码)

$(".okay").off();
$(".cancel").off();
$(".timeout").delay(1000).css('visibility', 'visible');
$(".okay").delay(1000).css('visibility', 'visible');
$(".cancel").delay(1000).css('visibility', 'visible');
$('.progress .progress-bar').delay(1000).progressbar({ display_text: 'fill' });


/*************************************************************************
*     User Selects Nothing
**************************************************************************/
var timeoutId = setTimeout(function () {

    $(".really").replaceWith('<div class="really">Confirmed.</div>');
    $('.cancel').fadeTo(300, 0, function () {
        $(".cancel").css('visibility', 'hidden');
    });

    $(".okay").animate({
        marginRight: "-=300px"
    }, 800).animate({
        opacity: 0
    }, 300);

    $("div").promise().done(function () {
        $('.timeout').fadeTo(100, 0, function () {
            $(".timeout").css('visibility', 'hidden');
        });
    });

    $(".dimbackground-curtain").delay(1200).fadeOut(1000);
    updateUserState(ui, state, id);
}, 3100);

/*************************************************************************
*     User clicks OK
**************************************************************************/

$(".okay").on("click", function () {
    clearTimeout(timeoutId);
    $(".really").replaceWith('<div class="really">Confirmed.</div>');
    $('.cancel').fadeTo(300, 0, function () {
        $(".cancel").css('visibility', 'hidden');
    });

    $(".okay").animate({
        marginRight: "-=300px"
    }, 800).animate({
        opacity: 0
    }, 300);

    $("div").promise().done(function () {
        $('.timeout').fadeTo(100, 0, function () {
            $(".timeout").css('visibility', 'hidden');
        });
    });

    $(".dimbackground-curtain").delay(1200).fadeOut(1000);
    updateUserState(ui, state, id);

});


/*************************************************************************
*     User clicks cancel
**************************************************************************/

$(".cancel").on("click", function () {
    clearTimeout(timeoutId);
    $(".really").replaceWith('<div class="really">Cancelled.</div>');
    $('.okay').fadeTo(300, 0, function () {
        $(".okay").css('visibility', 'hidden');
    });

    $(".cancel").animate({
        marginLeft: "-=300px"
    }, 800).animate({
        opacity: 0
    }, 300);

    $("div").promise().done(function () {
        $('.timeout').fadeTo(100, 0, function () {
            $(".timeout").css('visibility', 'hidden');
        });
    });

    $(".dimbackground-curtain").delay(1200).fadeOut(1000);

});

Things I've tried: 我尝试过的事情:

There are similar posts to this, one being " jQuery .on() only works once ", which suggest using a delegate. 有与此类似的帖子,一个是“ jQuery .on()仅可使用一次 ”,这建议使用委托。 I tried using .delegate() , . 我尝试使用.delegate(),。 live() and .one() . live().one() After reading some documentation I've also included the .off() methods, which I've tried in various places including at the top of the chain. 在阅读了一些文档之后,我还包括了.off()方法,我已经在各种地方尝试过了,包括在链的顶部。 I know this is probably something really simple that I'm missing. 我知道这可能是我所缺少的非常简单的东西。

Due to some security concerns 'on' only really functions properly on elements that are included in the dom when it's initially loaded. 由于一些安全方面的考虑,“ on”仅在最初加载dom时包含的元素上才能真正正常运行。 Try changing your call to this: 尝试将您的呼叫更改为此:

$('#containerElement').on('click', '.okay', function(){
    //click event code
});

Where containerElement is the element that this code is being added to and is present when the page is initially loaded. 其中containerElement是此代码要添加到的元素,并且在最初加载页面时存在。

Since it looks like you're adding and removing the confirmation banner you may want to have a permanent container that toggles between being visible and hidden which contains your ok button. 由于您似乎要添加和删除确认横幅,因此您可能需要一个永久容器,该容器在显示和隐藏之间切换,其中包含确定按钮。

Here's a fiddle that sort of illustrates the problem: http://jsfiddle.net/z3vk12mz/1/ 这是一个说明问题的小提琴: http : //jsfiddle.net/z3vk12mz/1/

I was fading the container divs to 0 and never resetting it . 我正在将容器div衰减为0,并且从未对其进行重置

 $("div").promise().done(function () {
        $('.timeout').fadeTo(100, 0, function () {
            $(".timeout").css('visibility', 'hidden');
        });
    });

adding this to the top of the code fixed the issue: 将其添加到代码顶部可解决此问题:

$(".timeout").delay(1000).css('visibility', 'visible');
$(".timeout").css('opacity', '1');

$(".okay").delay(1000).css('visibility', 'visible');
$(".okay").css('opacity', '1');
$(".okay").css('margin', 'initial');

$(".cancel").delay(1000).css('visibility', 'visible');
$(".cancel").css('opacity', '1');
$(".cancel").css('margin', 'initial');

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

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