Javascript 不等待使用 bootstrap-wizard.js 的 ajax

标签 javascript php jquery ajax twitter-bootstrap

我有一个bootstrap-wizard如下:

<div class="wizard-card" data-cardname="credential">
            <h3>Credential</h3>
            <div class="wizard-input-section">
                <p>Username</p>
                <div class="form-group">
                    <div class="col-sm-6">
                        <input type="text" class="form-control" id="Busername" name="Busername" placeholder="Username" required data-parsley-type="alphanum" data-validate="checkNameAvailable" data-msg="Alphanumeric allowed only" />
                    </div>
                </div>
                <p>Password</p>
                <div class="form-group">
                    <div class="col-sm-6">
                        <input type="password" class="form-control" id="Bpassword" name="Bpassword" placeholder="Password" />
                    </div>
                </div>
                <p>Re-type Password</p>
                <div class="form-group">
                    <div class="col-sm-6">
                        <input type="password" class="form-control" id="Bpassowrd2" name="Bpassword2" placeholder="Retype Password"/>
                    </div>
                </div>
            </div>
        </div>

输入<input type="text" class="form-control" id="Busername" name="Busername" placeholder="Username" required data-parsley-type="alphanum" data-validate="checkNameAvailable" data-msg="Alphanumeric allowed only" />调用checkNameAvailable函数进行验证。

检查NameAvailable函数: 它会调用 ajax 来检查该名称是否可用。

function checkNameAvailable(el){
        if($("#"+$(el).attr('id')).parsley().isValid()){
            var data =  $(el).val();
            if($(el).attr('id') == "Busername"){
                var type = "B";
            }else{
                var type = "I";
            }
            $.ajax({
                  method: "POST",
                  url: "isUserAvailableCommon",
                  async: false,
                  data: {Busername: data, _token: $("#_token").val(), _type: type },
                    success: function(msg) {
                        var retValue = {};
                        if(msg == "OK"){
                            retValue.status = true;
                            console.log(retValue);
                            return retValue;
                        }else{
                            retValue.status = false;
                            console.log(retValue);
                            return retValue;
                        }
                    }

             });
    }
}

问题在于变量retValue不会返回到 Bootstrap 向导验证。

但是,如果我这样尝试,它会起作用,但在使用 ajax 实现时则不起作用

function checkNameAvailable(el){
  var retValue = {};
  retValue.status = false;
  return retValue;
}

知道如何让它与ajax一起工作吗?我已经尝试过 Javascript function not waiting for AJAX response 中描述的回调和方法但仍然无法正常工作。

最佳答案

您可以尝试像这样更改您的功能:

function checkNameAvailable(el){
        var dfd = $.Deferred();

        if($("#"+$(el).attr('id')).parsley().isValid()){
            var data =  $(el).val();
            if($(el).attr('id') == "Busername"){
                var type = "B";
            }else{
                var type = "I";
            }

            $.ajax({
                  method: "POST",
                  url: "isUserAvailableCommon",
                  data: {Busername: data, _token: $("#_token").val(), _type: type },
                    success: function(msg) {
                        var retValue = {};
                        if(msg == "OK"){
                            retValue.status = true;
                            console.log(retValue);
                            dfd.resolve(retValue);
                        }else{
                            retValue.status = false;
                            console.log(retValue);
                            dfd.resolve(retValue);
                        }
                    }

             });
    }

    return dfd.promise();
}

// after this you can use 
// checkNameAvailable in this 
// way. 

checkNameAvailable(el).done(function(retValue) {
    console.log(retValue);
});

阅读我的评论和函数使用示例。当我们使用 Promise 时 该代码可以是异步的。你不能 async: false - 这不是 非常好的主意。

编辑: 同步ajax请求不是一个好主意,但现有代码 基本期望同步函数,所以你的函数必须是 改成这样:

function checkNameAvailable(el) {
    var result;

    if ($("#" + $(el).attr('id')).parsley().isValid()) {
        var data = $(el).val();
        if ($(el).attr('id') == "Busername") {
            var type = "B";
        } else {
            var type = "I";
        }
        $.ajax({
            method: "POST",
            url: "isUserAvailableCommon",
            async: false,
            data: {
                Busername: data,
                _token: $("#_token").val(),
                _type: type
            },
            success: function(msg) {
                var retValue = {};
                if (msg == "OK") {
                    retValue.status = true;
                    console.log(retValue);
                    result = retValue;
                } else {
                    retValue.status = false;
                    console.log(retValue);
                    result = retValue;
                }
            }

        });
    }

    return result;
}

祝你好运。

编辑2:

我已经 fork 了项目存储库并添加了如何使用的示例 异步进入验证器函数。这个解决方案有点脏 hack 但比使用同步逻辑要好得多。

https://github.com/gonaumov/bootstrap-application-wizard/blob/master/demo/demoAsynchronousValidator.html

我还针对此更改提出了拉取请求。

关于Javascript 不等待使用 bootstrap-wizard.js 的 ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29717827/

相关文章:

javascript - 使用边距后标题无法正确显示

javascript - 获取对象的构造函数名称

php - 排除任何与 MYSQL 全文搜索无关的内容

php - 这个数据库调用有什么问题?

javascript - 如何访问 sapper/polka 中所有 svelte 组件的 json 数据文件

javascript - 让每个按钮操作其上方的内容

php - PHP 支付错误代码 13113 中的 Paypal API Express Checkout

php - 在 php 中使用 session.use_cookies

jquery - 在可变高度的固定页眉和页脚之间滚动内容

javascript - 正则表达式: transferring the data attribute