【问题标题】:return false not working in localstoragedb返回 false 在 localstoragedb 中不起作用
【发布时间】:2017-04-19 04:52:37
【问题描述】:

我将localstoragedb 用于我的本地数据库,到目前为止,我所做的一切都是正确的。但是在提交表单之一时,return false 不会停止提交表单。 这是代码.....

$("#registration").on("submit", function(e) {
    e.preventDefault();
    if ($('#username').val() === '') {
        $('#username').parent().addClass('error');
        return false;
    };
    if ($('#password').val() === '') {
        $('#password').parent().addClass('error');
        return false;
    };
    if ($('#mobile').val() === '') {
        $('#mobile').parent().addClass('error');
        return false;
    };

    localDb.queryAll("UserInfo");
    localDb.queryAll("UserInfo", {
        query: function(row) {
            if (row.username === $('#username').val()) {
                alert("username already existed");
                return false;
            }

        }

    });


    localDb.insert("UserInfo", {
        username: $('#username').val(),
        password: $('#password').val(),
        Mobile: $("#mobile").val()
    });
    localDb.commit();
    $(".message").show();
    setTimeout(function() {
        window.location.replace("login.html");
    }, 2000);

});

除了localDb.queryAll 函数之外,所有的 return false 都可以正常工作。 它会提醒我用户名已经存在,但也提交了表单。 return false 看起来有些问题。 我有什么遗漏或做错了吗???? 提前致谢…………

【问题讨论】:

    标签: javascript jquery local-storage


    【解决方案1】:

    我在循环中犯了一个错误,因为它继续在每一行中搜索特定值,并且找不到它插入它的位置。这是正确的代码....

    $("#registration").on("submit", function(e) {
        e.preventDefault();
        if ($('#username').val() === '') {
            $('#username').parent().addClass('error');
            return false;
        };
        if ($('#password').val() === '') {
            $('#password').parent().addClass('error');
            return false;
        };
        if ($('#mobile').val() === '') {
            $('#mobile').parent().addClass('error');
            return false;
        };
    
        localDb.queryAll("UserInfo");
        localDb.queryAll("UserInfo", {
            query: function(row) {
                if (row.username === $('#username').val()) {
                    alert("username already existed");
                    return false;
                }
    
            }
    
        });
    
    
        localDb.insert("UserInfo", {
            username: $('#username').val(),
            password: $('#password').val(),
            Mobile: $("#mobile").val()
        });
        localDb.commit();
        $(".message").show();
        setTimeout(function() {
            window.location.replace("login.html");
        }, 2000);
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-13
      • 2018-12-05
      • 1970-01-01
      • 2015-02-28
      • 1970-01-01
      • 1970-01-01
      • 2012-01-27
      相关资源
      最近更新 更多