2017-04-19 66 views
0

我使用localstoragedb爲我的本地數據庫,直到現在無論我所做的工作是正確的。但是在提交表單時返回false並不會停止提交表單。 下面是代碼.....返回false不工作在localstoragedb

$("#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有些問題。 是我失蹤或做錯了嗎? 在此先感謝.........

回答

0

我在循環中犯了一個錯誤,因爲它繼續搜索每一行中的特定值,並且它在哪裏找不到它插入它。這裏是正確的代碼....

$("#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); 

}); 
相關問題