问题 jQuery向导步骤在ajax调用完成之前移动


如何根据某些ajax调用的结果控制移动到下一步? data.d以bool值返回

$("#wizard").steps({
            onStepChanging: function (event, currentIndex, newIndex) {
                var move = false;
                if (currentIndex == 2) {
                    move = false;
                    $.ajax({
                        type: 'POST',
                        url: "Reservation.aspx/SomeFunction",
                        data: serializeData({  }),
                        contentType: "application/json",
                        dataType: 'json',
                        success: function (data) {
                            move = data.d;
                            return true;
                        },
                        error: ajaxLoadError
                    });
                }
                return move;
            },
            saveState: true

        });

1033
2017-11-25 20:51


起源

我没有使用jquery向导步骤但是你的问题发生是因为ajax被异步调用。查看另一个接受ajax加载的向导插件(如: github.com/mstratman/jQuery-Smart-Wizard) - Gasim


答案:


$.ajax({
    type: 'POST',
    url: "Reservation.aspx/SomeFunction",
    async: false,
    contentType: "application/json",
    dataType: 'json',
    success: function (data) {
       move = data.d;
       return true;
    },
    error: ajaxLoadError
});

4
2017-11-25 23:13



似乎没有办法使它异步工作,所以我会奖励你的赏金,因为这是我发现让它工作的唯一方法。 - Manolo


如果您不希望$ .ajax()函数立即返回,请将async选项设置为false:

如果服务器没有响应您的ajax调用,则为Ajax设置超时,然后它将继续进行下一个进程。

$("#wizard").steps({
            onStepChanging: function (event, currentIndex, newIndex) {
                var move = false;
                if (currentIndex == 2) {
                    move = false;
                    $.ajax({
                        type: 'POST',
                        url: "Reservation.aspx/SomeFunction",
                        data: serializeData({  }),
                        contentType: "application/json",
                        dataType: 'json',
                        async: false,
                        cache: false,
                        timeout: 30000,
                        success: function (data) {
                            move = data.d;
                            return true;
                        },
                        error: ajaxLoadError
                    });
                }
                return move;
            },
            saveState: true

        });

3
2018-03-11 10:23



正如我所说,“我想知道是否有任何ASYNC方法”。 - Manolo
是的我读过这个但是,我忘了提到没有ASYNC就很难处理它。可以使用setTimeout(function(){“你的AJAX代码”},1000);但这不是安全的处理方式。如果您对ASYNC没有任何问题,以上解决方案是安全且安全的。 - Nirav Patel
你从上面的答案得到了帮助吗? - Nirav Patel
不,这是我的实际方法。我正在寻找一种异步方法。但是,我不确定这个插件是否可行。如果没有,我会继续设置 async 至 false。无论如何,赏金是针对异步方法的。 - Manolo
看看@Samy的回答。他回答的基本和你一样。 - Manolo


你可以使用Samy的方法同步ajax请求

$("#wizard").steps({
    onStepChanging: function (event, currentIndex, newIndex) {
        if (currentIndex == 2) {
            var requestResult = $.ajax({
                type: 'POST',
                url: "Reservation.aspx/SomeFunction",
                async: false,
                contentType: "application/json",
                dataType: 'json',
                error: ajaxLoadError
            });
            return requestResult.responseJSON.Result == "/*your expected value*/"
        }
    },
    saveState: true
});

2
2018-06-24 15:59





$("#wizard").steps({
        onStepChanging: function (event, currentIndex, newIndex) {
            var $out= false;
            if (currentIndex == 2) {
                $out= false;
                $.ajax({
                    type: 'POST',
                    url: "Reservation.aspx/SomeFunction",
                    data: serializeData({  }),
                    contentType: "application/json",
                    dataType: 'json',
                    success: function (data) {
                        move = data.d;

                        $out = true;
                    },
                    error: ajaxLoadError
                });
            }
            return $out;
        },
        saveState: true

    });

把全局变量$放出去!


1
2017-10-15 07:22





var items;

$("#wizard").steps({
onStepChanging: function (event, currentIndex, newIndex) {
    var move = false;
    if (currentIndex == 2) {
        move = false;

        items = $.ajax({
            type: 'POST',
            url: "Reservation.aspx/SomeFunction",
            data: serializeData({  }),
            contentType: "application/json",
            dataType: 'json',
            success: function (data) {
                move = data.d;
                return true;
            },
            error: ajaxLoadError
        });


    }
    return move;
},
saveState: true

});



items.success(function (data) {
//if can log in go to logged in page
if (data.success == true) {
    alert("Working");
    var move = data.d;
    return true;

} else {
    alert("didnt work");
}
// output of data
alert(JSON.stringify(data));
});

1
2018-03-15 15:54





我有同样的问题,我甚至想在ajax加载后使用“setStep”强制执行步骤,然后最新版本的jquery.steps取出了“setStep”..

我最后使用“next”方法,并且必须使用全局触发器来停止onChanging事件的无限循环,简而言之,如果ajax返回有效数据,我强制向导转到下一步,否则,它会保持到当前步骤,这是代码

var $stopChanging = false; 

.... ....

onStepChanging: function (event, currentIndex, newIndex) {
      if ($stopChanging) {
        return true;
      } else {
        items = $.ajax({
        type: 'POST',
        url: "Reservation.aspx/SomeFunction",
        data: serializeData({  }),
        contentType: "application/json",
        dataType: 'json',
        success: function (data) {
            $stopChanging = true;
            wizard.steps("next");
        },
        error: ajaxLoadError
    });
   },
   onContentLoaded: function (event, currentIndex) {
       $stopChanging = false;
   }
}

逻辑如下:

  1. 单击“下一步”按钮以触发onStepChanging
  2. 默认情况下,设置jquery.steps onStepChanging事件返回false,然后是$ .ajax调用,如果它返回有效数据(成功),则调用jquery.steps到 转到下一步,onStepChanging再次触发,如果不是 有效,什么都不做,保持现阶段

每次触发两个onStepChanging事件听起来不是一个好主意,但这就是我现在可以拥有的

您可能需要为不同的步骤索引行为添加更多条件


1
2018-01-15 23:35



这看起来不是一个完整的答案。 - RBT


这是我在几次尝试之后能够开始工作的唯一方法,这就是@ joe-lu在上面得到的。您只想启动异步调用并返回false。这将使向导保持同一步。然后在成功处理程序中,您以编程方式转到下一步。

$("#wizard").steps({
            onStepChanging: function (event, currentIndex, newIndex) {
                if (currentIndex == 2) {
                    //Would be a good idea to start a spinner here!
                    //would be a good idea to disable next button here!
                    $.ajax({
                        type: 'POST',
                        url: "Reservation.aspx/SomeFunction",
                        data: serializeData({  }),
                        contentType: "application/json",
                        dataType: 'json',
                        success: function (data) {
                            //stop spinner here!
                            //programmatically move to next step on success.
                            $("#wizard").steps("next");
                        },
                        error: ajaxLoadError
                    });
                }
                //Prevents natural movement to next step.
                //will be done programmatically
                return false;
            },
            saveState: true
        });

1
2018-01-24 22:41





我遇到了类似的问题,但我使用parsleyjs进行验证。你可能会在我的代码中得到一个想法。

我的代码是这样的:

             onStepChanging: function (event, currentIndex, newIndex) {

                 // ======== Code that fails 

                 //var step = $wizard_advanced.find('.body.current').attr('data-step'),
                 //$current_step = $('.body[data-step=\"'+ step +'\"]');                        


                // check input fields for errors
                //$current_step.find('[data-parsley-id]').each(function() {
                    //this adds .md-input-danger to inputs if invalid
                    //there is remote validation occurring here via ajax
                    // async: false
                    //$(this).parsley().validate();
                //});

                // this is executed before ajax validation is finished 
                //return $current_step.find('.md-input-danger').length ? false : true;

                // ======== END of Code that fails 

                // FIX
                // waits on ajax validation to finish before returning
                if( $wizard_advanced_form.parsley().validate() ) {
                    return true;
                } else {
                    return false;
                }
                //FIX                    
            }

0
2018-01-20 13:37





var is_async_step = false;
$("#wizard").steps({
        onStepChanging: function (event, currentIndex, newIndex) {
            //USED TO SEND USER TO NEXT STEP IS ASYNC REQUEST IS PRESENT - FOR AJAX CALL 
            if (is_async_step) {
                is_async_step = false;
                //ALLOW NEXT STEP
                return true;
            }

            if (currentIndex == 2) {                
                $.ajax({
                    type: 'POST',
                    url: "Reservation.aspx/SomeFunction",
                    data: serializeData({  }),
                    contentType: "application/json",
                    dataType: 'json',
                    success: function (data) {
                        move = data.d;

                        //Add below 2 lines for every Index(Steps).                            
                        is_async_step = true;
                        //This will move to next step.
                        $(form).steps("next");
                    },
                    error: ajaxLoadError
                });
            }
             //Return false to avoid to move to next step
             return false;
        },
        saveState: true
    });

0
2017-07-05 12:17