Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/478.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我的jQuery ajax if/else on click不起作用_Javascript_Jquery_Html_Ajax - Fatal编程技术网

Javascript 我的jQuery ajax if/else on click不起作用

Javascript 我的jQuery ajax if/else on click不起作用,javascript,jquery,html,ajax,Javascript,Jquery,Html,Ajax,所以我一直在这个问题上纠缠了很长时间。基本上,我在index.html中有一个按钮(#action)。我有第二个页面:number.html。我正在尝试从index.html中获取.receiver范围,从number.html中获取.success内容或.failure内容,具体取决于是否在2秒内单击了#action 代码如下: $(function() { var ajaxRetrieve = function(callback) { $.ajax({

所以我一直在这个问题上纠缠了很长时间。基本上,我在index.html中有一个按钮(#action)。我有第二个页面:number.html。我正在尝试从index.html中获取.receiver范围,从number.html中获取.success内容或.failure内容,具体取决于是否在2秒内单击了#action

代码如下:

$(function() {

    var ajaxRetrieve = function(callback) {
        $.ajax({
            url: 'number.html',
            method: 'POST',
            success: function(responseData) {
                callback(responseData);
            },
            error: function(responseData) {
                alert('Check yourself');
            }
        });
    }

    var flag = 0;

    $('#action').on('click', function() {
        flag = 1;
    });

    if (flag == 1) {
        ajaxRetrieve(function(data) {
            $('.receiver').html($(data).find('.success'));
        });
    } else {
        setTimeout(function() {
            ajaxRetrieve(function(data) {
                $('.receiver').html($(data).find('.failure'));
            });
        }, 3000);
    };


});

问题:单击时,我从未获得.success内容,并且没有错误消息。但2秒钟后,故障就出现了。我试过几种方法让它工作,但都没有用。我还用一个警告框检查了标志值是否在单击时更改,它是

您需要在
on click
函数中包含ajax调用,否则
if
逻辑将仅在加载页面时调用,不再调用

$(function() {

    var ajaxRetrieve = function(callback) {
        $.ajax({
            url: 'number.html',
            method: 'POST',
            success: function(responseData) {
                callback(responseData);
            },
            error: function(responseData) {
                alert('Check yourself');
            }
        });
    }

    var flag = 0;

    $('#action').on('click', function() {
        flag = 1;
        flagCheck();
    });

    var flagCheck = function() {
        if (flag == 1) {
            ajaxRetrieve(function(data) {
                $('.receiver').html($(data).find('.success'));
            });
        } else {
            setTimeout(function() {
                ajaxRetrieve(function(data) {
                    $('.receiver').html($(data).find('.failure'));
                });
            }, 3000);
        };
    }

});

即使未单击,代码也将始终运行