HTML 文件:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/index.css">
<title>SqliteSampleForm</title>
</head>
<body>
<h1>Sample Sqlite Database Form</h1>
<p align="center">
<input type="text" id="name" placeholder="Enter First Name" name="firstname" />
<input type="text" id="name" placeholder="Enter Last Name" name="lastname" />
<input type="text" id="name" placeholder="Enter contact number" name="contactnumber" /> <br /><br />
<button onclick="add()"> submit </button>
</p>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="scripts/platformOverrides.js"></script>
<script type="text/javascript" src="scripts/index.js"></script>
</body >
</html >
这是我的 index.js 文件:
(function () {
"use strict";
var db = null;
document.addEventListener("deviceready", function () {
db = window.sqlitePlugin.openDatabase({ name: "note.db", iosDatabaseLocation: 'default' });
db.transaction(function (tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS note (firstname text primary key, lastname text, contactnumber text)");
alert("Database Table created");
}, function (err) {
alert("An error occured while initializing the app");
});
}, false);
function add() {
alert("Inside Add Function");
var firstname = document.getElementByName("firstname").value;
alert("Variabe firstname created: " + firstname);
var lastname = document.getElementByName("lastname").value;
var contactnumber = document.getElementsByName("contactnumber").value;
if (firstname == "") {
alert("Please enter first name");
return;
}
if (lastname == "") {
alert("Please enter last name");
return;
}
if (contactnumber == "") {
alert("Please enter contact number");
return;
}
db.transaction(function (tx) {
tx.executeSql("INSERT INTO note (firstname, lastname, contactnumber) VALUES (?,?,?)", [firstname, lastname, contactnumber], function (tx, res) {
alert("Fields Added to the database");
});
}, function (err) {
alert("An error occurred while saving the note");
});
}
} )();
并且可能不需要 css 文件,所以为了避免过多的代码,我会避免使用它。
单击提交按钮后,我应该会收到警告,提示我在函数定义中所述的添加函数中,但我没有收到。 当我在我的应用程序中获得数据库创建警报时。 这个错误是由于我的应用程序中的 sqlite 数据库导致的吗?
最佳答案
经过大量尝试后,我发现如果您的 javascript 文件中存在错误(语法错误或任何其他错误),则 java 脚本文件不会加载。
删除错误后,您将能够根据需要运行脚本
关于javascript - javascript 函数上的 HTML 按钮调用不适用于 sqlite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44583175/