JavaScript函数不会触发

时间:2016-03-16 18:58:51

标签: javascript jquery function cordova jquery-mobile

我有两个主要功能,除了变量名和它正在使用的表之外几乎完全相同。

我正在使用phonegap,html,javascript,JQuery Mobile。

函数eventSoccer在第一个警报出现时被访问,但数据输入没有被存储,而页面只是刷新。

函数registerUser工作得很好,显示sql查询并显示警告'用户已注册',数据在数据库中。

任何人都可以确定为什么函数eventSoccer似乎不起作用?由于这两个功能几乎完美无瑕。

eventSoccer -

function eventSoccer() {
    alert("eventSoccer hit");
    var TitleT = document.getElementById("txttitle").value;
    var LocationL = document.getElementById("txtlocation").value;
    var PeopleP = document.getElementById("txtpeople").value;
    var DateD = document.getElementById("txtdate").value;
    var DescriptionD = document.getElementById("txtdesc").value;

    db = window.openDatabase("SoccerEarth", "2.0", "SoccerEarthDB", 2 * 1024 * 1024);
    db.transaction(function(tx) {
        NewSoccer(tx, TitleT, LocationL, PeopleP, DateD, DescriptionD);
    }, errorEvent, successEvent);
}

function NewSoccer(tx, TitleT, LocationL, PeopleP, DateD, DescriptionD) {
    var _Query = ("INSERT INTO SoccerEvents(Title, Location, NoPeople, Date, Description) values ('" + TitleT + "','" + LocationL + "','" + PeopleP + "','" + DateD + "', '" + DescriptionD + "')");
    alert(_Query);
    tx.executeSql(_Query);
}

function errorEvent(error) {
    navigator.notification.alert(error, null, "Event not created", "cool");
}

function successEvent() {
    navigator.notification.alert("The event has now been created", null, "Information", "ok");
}

registerUser -

function registerUser() {
    var Username = document.getElementById("txtusername").value;
    var Firstname = document.getElementById("txtfirstname").value;
    var Lastname = document.getElementById("txtlastname").value;
    var Email = document.getElementById("txtemail").value;
    var Password = document.getElementById("txtpassword").value;
    var Confirmpass = document.getElementById("passwordconfirm").value;

    db = window.openDatabase("SoccerEarth", "2.0", "SoccerEarthDB", 2 * 1024 * 1024);
    db.transaction(function(tx) {
        NewUser(tx, Username, Firstname, Lastname, Email, Password, Confirmpass);
    }, errorRegistration, successRegistration);
}

function NewUser(tx, Username, Firstname, Lastname, Email, Password, Confirmpass) {
    var _Query = ("INSERT INTO SoccerEarth(UserName, FirstName, LastName, Email, Password, CPass) values ('" + Username + "','" + Firstname + "','" + Lastname + "','" + Email + "', '" + Password + "', '" + Confirmpass + "')");
    alert(_Query);
    tx.executeSql(_Query);
}

function errorRegistration(error) {
    navigator.notification.alert(error, null, "Got an error mate", "cool");
}

function successRegistration() {
    navigator.notification.alert("User data has been registered", null, "Information", "ok");
}

HTML -

<div data-role="content">
    <br>
    <form>
        <label for="txttitle">Title</label>
        <input type="text" id="txttitle">
        <br>
        <label for="txtlocation">Location</label>
        <input type="text" id="txtlocation">
        <br>
        <label for="txtpeople">Number Of People</label>
        <input type="text" id="txtpeople">
        <br>
        <label for="txtdate">Date</label>
        <input type="text" id="txtdate">
        <br>
        <label for="textdesc">Description</label>
             <textarea name="textarea" id="textdesc"></textarea>
        <br>
        <input type="submit" value="submit" onclick="return eventSoccer()">
    </form>
</div>

1 个答案:

答案 0 :(得分:2)

我认为它在这里打破了:

var DescriptionD = document.getElementById("txtdesc").value;

document.getElementById(&#34; txtdesc&#34;)= null,因为没有&#39; txtdesc&#39;在您的HTML中(您没有 e xtdesc&#39;)

因此我们有document.getElementById(&#34; txtdesc&#34;)。value&lt; =&gt; null .value - 肯定会抛出错误,并且会在正常执行代码时抛出中断。

顺便说一下 - 我很困惑这实际上是如何运作的:

<input type="submit" value="submit" onclick="return eventSoccer()">

我至少会以这种形式写出来:

<input type="submit" value="submit" onclick="eventSoccer(); return false;">

所以提交点击不会发送表单?或者您的意图是发送表格?无论如何 - 这是你在这件事上的选择。