[英]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.