javascript - javascript 函数上的 HTML 按钮调用不适用于 sqlite 数据库

标签 javascript jquery html css sqlite

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/

相关文章:

jquery - 访问 WordPress 的 TinyMCE iFrame

html - 根据值更改输入的样式

php - 将 HTML 表单值作为数组放入 SESSION 变量中

JavaScript 在第一个括号后插入文本

javascript - 重用 FileReader 时宽度和高度错误

jquery - JQGrid真实滚动问题

php - 如何使用javascript提交没有表单的组合框值?

javascript - 简单的 NPM 包捆绑/销售?

javascript - 如何实现D3圆环图?

javascript - Google IMA SDK 的 HTML 5 问题