我是JavaScript的新手,正在制作一个由注册表组成的餐厅应用程序。我想检查用户名是否已经存在,如果它确实存在,它应该在按钮点击时向用户发出警报。
JavaScript代码:
<script type="text/javascript">
var db;
var shortname="R_loginDB";
var version = "1.0";
var displayName = "R_loginDB";
var maxSize = 90000;
function onBodyLoad() {
db = openDatabase(shortname,version,displayName,maxSize);
db.transaction(function(tx){
tx.executeSql('CREATE TABLE IF NOT EXISTS R(UserName TEXT NOT NULL PRIMARY KEY,Password TEXT NOT NULL,ConPassword TEXT NOT NULL)');
});
}
function ListDBValues() {
if (!window.openDatabase) {
alert('Databases are not supported in this browser.');
return;
}
$('#output').html('');
db.transaction(function(transaction) {
transaction.executeSql('SELECT * FROM R;', [], function(transaction, result) {
if (result != null && result.rows != null) {
for (var i = 0; i < result.rows.length; i++) {
var row = result.rows.item(i);
$('#output').append('<br>' + ' ' + row.UserName+ ' ' + row.Password + ' '+ row.ConPassword);
}
}
});
});
return;
}
ListDBValues();
function AddValues() {
var flag = false;
if ($("#pass").val() != $("#conpass").val()) {
alert("Passwords do not match.");
window.location.href = "r_register.html";
} else if (flag == false) {
try {
db.transaction(function(transaction) {
transaction.executeSql('INSERT INTO R(UserName, Password, ConPassword) VALUES (?,?,?)',[$('#uname').val(),$('#pass').val(),$('#conpass').val()]);
});
flag = true;
window.location.href = "login.html";
} catch(err) {
alert("Username " + $("#uname").val() + " is already taken!! Please chose another one");
window.location.href = "r_register.html";
}
}
}
AddValues();
</script>
HTML code:
<body onload="onBodyLoad()">
<div id="main">
<img id="bg" src="file:///android_asset/www/images/bigtable_1_blur.jpg"/>
<input id="uname" type="text" placeholder=" Username"></input>
<input id="pass" type="password" placeholder=" Password"></input>
<input id="conpass" type="password" placeholder=" Confirm Password"></input>
<a href="login.html"><p id="login"><b>Already have an account?<i><u>Login</u></i></b></p></a>
<!-- <a href="second.html"> -->
<input id="btn" type="button" value="Register" onclick="AddValues()"></input><br/>
<!-- </a> -->
<input id="btn2" type="button" value="show" onclick="ListDBValues()"/></input>
<span id="output"></span>
</div>
</body>
答案 0 :(得分:2)
WebSQL API已被删除。它不太可能在目前不支持它的平台上得到支持,并且可以从那些支持平台的平台上删除。
尝试使用localStorage。点击Cordova storage documentation
了解详情