我创建了一个函数,它应该通过jQuery从html中的输入字段获取值,并将它们插入到WebSQL的表中。它不起作用,我从输入字段中获取值肯定(记录它们),我的功能连接到html中的按钮(也已选中)。不知道是什么问题。
Main.js文件:
$(document).ready(function(){
//Open db
var db = openDatabase('customers', '1.0', 'Test DB', 2 * 1024 * 1024);
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS customers (name, email unique)');
});
})
function addCustomer(){
var customerName = $("#name").val();
var customerEmail = $("#email").val();
db.transaction(function(tx){
tx.executeSql('INSERT INTO customers (name, email) VALUES (?, ?)', [customerName, customerEmail]);
});
}
Html输入字段和按钮:
<form onsubmit="addCustomer()">
<div class="form-group">
<label>Customer Name</label>
<input type="text" id="name" class="form-control" placeholder="Enter Name">
</div>
<div class="form-group">
<label>Customer Email</label>
<input type="email" id="email" class="form-control" placeholder="Enter Email">
</div>
<button type="submit" class="btn btn-default" onclick="addCustomer()">Add Customer</button>
</form>