简体   繁体   English

Javascript:如果稍后重定向到另一个页面,则无法将数据添加到数据库

[英]Javascript: Can't add data to database if redirecting to another page later

I'm having a very strange problem with Javascript I can fill in the form, and then click Save to save data from form to the database successfully But if I add window.location.href = "deadlines.html" to the end of the function that insert to DB, to redirect to that page, the data will not be saved to db我在使用 Javascript 时遇到了一个非常奇怪的问题我可以填写表单,然后单击保存以成功将表单中的数据保存到数据库但是如果我将window.location.href = "deadlines.html"添加到末尾插入到数据库的函数,重定向到该页面,数据不会保存到数据库

This is the code: http://jsfiddle.net/ajSte/这是代码: http : //jsfiddle.net/ajSte/

<form id="formDeadlineInfo">
        <label for="shortDescription">Short Description</label>
        <input id="shortDescription"  type="text">
        <br>
        <label for="class">Class</label>
        <select id="class" class="ui-selectmenu" >
          <option value="HCI">HCI</option>
          <option value="DataMining">Data Mining</option>
          <option value="MobileDev">Mobile Dev</option>
          <option value="Ethics">Ethics</option>
        </select>
        <br>
        <label for="dueDate">Due Date</label>
        <input id="dueDate" type="date">
        <br>
        <label for="dueTime">Due Time</label>
        <input id="dueTime" type="time">
        <br>
        <label for="type">Type</label>
        <select id="type">
          <option value="Homework">Homework</option>
          <option value="Test">Test</option>
        </select>
        <br>
        <label for="additionalInfo">Additional Info</label>
        <input id="additionalInfo" type="text">
        <br>
        <label for="finished">Finished</label>
        <select data-role="flipswitch" id="finished" class="ui-selectmenu" >
          <option value="no" selected>No</option>
          <option value="yes">Yes</option>

        </select>

        <input type="button" class="ui-btn ui-btn-active ui-btn-icon-bottom" data-role="button" value="Save" style="text-align:center" onClick="getFormInfo()">

    </form>

and Javascript: http://jsfiddle.net/ajSte/ Sorry the "Code" function of this is hard to handle, please see in jsfiddle和 Javascript: http : //jsfiddle.net/ajSte/抱歉,这个“代码”功能很难处理,请参阅 jsfiddle

I opened and populated database already before those codes run so don't need to ask about it Thank u so much if u can help me solve problem我在这些代码运行之前已经打开并填充了数据库,所以不需要询问它如果你能帮我解决问题,非常感谢

But if I add window.location.href = "deadlines.html" to the end of the function that insert to DB, to redirect to that page, the data will not be saved to db但是如果我将 window.location.href = "deadlines.html" 添加到插入数据库的函数的末尾,重定向到该页面,数据将不会保存到数据库

It's not strange at all.这一点也不奇怪。 The insert operation is asynchronous , which means your code starts it, but then it finishes later (which is why it accepts a callback).插入操作是异步的,这意味着您的代码启动它,但稍后完成(这就是它接受回调的原因)。

If you tell the code to take the user away from the page, the asynchronous operation is cancelled before it completes.如果你告诉代码让用户离开页面,异步操作在它完成之前就会被取消。 You'll need to wait and then use that code writing to window.location.href within the success callback.您需要等待,然后在成功回调中使用该代码写入window.location.href

Eg:例如:

function insertDeadlineToDB(dbId,dbDescription,dbClass,dbDueDate, dbDueTime, dbType, dbAdditionalInfo, dbFinished) {
    //alert('insert called');

    //alert('before insert');
    db.transaction(function(tx){
        tx.executeSql(
            'INSERT INTO deadlines (id, description, class, dueDate, dueTime, type, additionalInfo, finished) VALUES (?,?,?,?,?,?,?,?)',
            [dbId,dbDescription,dbClass,dbDueDate, dbDueTime, dbType, dbAdditionalInfo, dbFinished], function() {
               successCB();
               window.href.location = "deadlines.html"; // <=== Here
            }, errorCB);
        ////alert(tx);
   });
   //window.href.location = "deadlines.html";              <=== Not here
}

Side note: The code has missing variables, such as successCB which isn't defined anywhere.旁注:代码缺少变量,例如未在任何地方定义的successCB

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 重定向到Cordova应用程序中的另一个页面后无法运行javascript - Can't run javascript after redirecting to another page in cordova app JavaScript 如果条件没有重定向到另一个页面 - JavaScript if condition isn't redirecting to another page 重定向到另一个无法在javascript中运行的页面 - Redirecting to another page not working in javascript 无法使用相对路径重定向到另一个页面 - Can't redirecting to another page using relative path Javascript:发布请求,然后将页面重定向到另一个页面 - Javascript : Posting a request and then redirecting the page to another page 在使用 JavaScript 重定向到另一个页面之前提交表单 - Submit form before redirecting to another page with JavaScript 如何找到javascript重定向到另一个页面的位置? - How to find where javascript is redirecting to another page? 重定向到 javascript 中的另一个页面时,LocalStorage 未更新 - LocalStorage is not updated when redirecting to another page in javascript AJAX无法在其他页面中加载JavaScript - AJAX can't load JavaScript in another page 我可以使用HTML / CSS和javascript创建登录页面,然后在其中添加VUE吗? - Can I create a login page with HTML/CSS and javascript and later add VUE to it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM