我的jQuery排队函数在第一次后无法运行 [英] My jQuery queued functions fail to run after the first time

查看:68
本文介绍了我的jQuery排队函数在第一次后无法运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<input id='lightson' class="buttonl" type="button" value="On" />
<input id='lightsoff'  class="buttonl" type="button" value="Off" />
<p class="status2"></p>

<script type="text/javascript">
$('#lightson').click( function(){
    $.get('http://<?php echo $_SESSION['ip']?>/?2', {}, callbacka());
    function callbacka(){
        $('.status2').load('status2.php').delay(3000).queue(function() {
            $(this).empty();
        });
    }
});

$( '#lightsoff' ).click( function () {
    $.get('http://<?php echo $_SESSION['ip']?>/?3', {}, callbackb());
    function callbackb(){
        $('.status2').load('status3.php').delay(3000).queue(function() {
            $(this).empty();
        });
    } 
});
</script>

当我单击lightson时,我想将某些内容加载到我的div中,并在三秒钟后将其清空.另外,当我单击lightsoff时,我想加载其他内容,并在三秒钟后将其清空,因为两个按钮都使用相同的div.

When I click lightson, I want to load something in to my div and after three seconds to empty it. Also when I click lightsoff I want to load something else and after three seconds to empty it because both buttons use the same div.

此代码仅工作一次.当页面加载并且我单击两个按钮之一(与哪个按钮无关)时,它可以工作,但是如果我单击另一个按钮,它将在div中加载信息,但不会将其清空.

This code works only once. When my page loads and I click one of two buttons (it doesn't matter which button) it works, but if I click the other button, it loads the info in the div but doesn't empty it.

目前适用的新代码是:

<input id='lightson' class="buttonl" type="button" value="On" />
<input id='lightsoff'  class="buttonl" type="button" value="Off" />
<p class="status2"></p>

<script type="text/javascript">
$('#lightson').click(function(){
    $.get('http://<?php echo $_SESSION['ip']?>/?2', {}, callbacka());
    function callbacka() {
        $('.status2').load('status2.php').delay(3000).queue(function(){
            $('.status2').empty().dequeue();
        });
    }
});

$('#lightsoff').click( function () {
    $.get('http://<?php echo $_SESSION['ip']?>/?3', {}, callbackb());
    function callbackb(){
       $('.status2').load('status3.php').delay(3000).queue(function() {
           $('.status2').empty().dequeue();
       });
    } 
});
</script>

上述代码的描述:我有两个按钮.当我单击其中的一个时,我想将值"2"发送到url并在类status2中发送以加载status2.php的内容,并在3秒后将类status2清空.依此类推,如果我单击其他按钮.我认为上面的代码可以完成这项工作. 如果单击按钮时从回调中删除()",则仅发送值"2",而不会发生其他任何事情.

Description for what I want with the above code: I have two button. When I click one of them I want to send the value "2" to a url and in the class status2 to load the content of status2.php and after 3secs to empty the class status2. And so on if I click the other button. I think the above code do the job. If I remove the "()" from the callback when I click the button only send the value "2" and nothing else happens.

推荐答案

<script type="text/javascript">
    $( '#lightson' ).click( function () {
            $.get( "http://<?php echo $_SESSION['ip']?>/?2" );
            callbacka();
    } );

     function callbacka() {           

                $( '.status2' ).load( 'status2.php' ).delay( 3000 ).queue( function() {
                    $('.status2').empty().dequeue();
                });

    }

    $( '#lightsoff' ).click( function () {
            $.get( 'http://<?php echo $_SESSION['ip']?>/?3' );
            callbackb();
    } );

    function callbackb() {

                $( '.status2' ).load( 'status3.php' ).delay( 3000 ).queue( function() {
                    $( '.status2' ).empty().dequeue();
                });

    }
</script>

这篇关于我的jQuery排队函数在第一次后无法运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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