我正在尝试在提交详细信息后重定向到其他页面。 我已经尝试了注释掉的代码。它确实重新指导BUT然后不接收数据。 当我发表评论时,它不会重定向,但它会接收数据。 我希望我能正确解释这一点。
有什么想法吗?
<script>
var curatio = {};
curatio.webdb = {};
curatio.webdb.db = null;
curatio.webdb.open = function() {
var dbSize = 5 * 1024 * 1024; // 5MB
curatio.webdb.db = openDatabase("Curatio", "1.0", "Todo manager", dbSize);
}
curatio.webdb.createTable = function() {
var db = curatio.webdb.db;
db.transaction(function(tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS weight(ID INTEGER PRIMARY KEY ASC, todo TEXT, added_on DATETIME, date TEXT, note TEXT )", []);
});
}
curatio.webdb.addTodo = function(todoText) {
var db = curatio.webdb.db;
db.transaction(function(tx){
var addedOn = new Date();
var date = document.getElementById("date").value;
var note = document.getElementById("note").value;
tx.executeSql("INSERT INTO weight(todo, added_on, date, note) VALUES (?,?,?,?)",
[todoText, addedOn, date, note],
curatio.webdb.onSuccess,
curatio.webdb.onError);
});
}
curatio.webdb.onError = function(tx, e) {
alert("There has been an error: " + e.message);
}
curatio.webdb.onSuccess = function(tx, r) {
// re-render the data.
curatio.webdb.getAllTodoItems(loadTodoItems);
}
curatio.webdb.getAllTodoItems = function(renderFunc) {
var db = curatio.webdb.db;
db.transaction(function(tx) {
tx.executeSql("SELECT * FROM weight", [], renderFunc,
curatio.webdb.onError);
});
}
curatio.webdb.deleteTodo = function(id) {
var db = curatio.webdb.db;
db.transaction(function(tx){
tx.executeSql("DELETE FROM weight WHERE ID=?", [id],
curatio.webdb.onSuccess,
curatio.webdb.onError);
});
}
function loadTodoItems(tx, rs) {
var rowOutput = "";
var todoItems = document.getElementById("todoItems");
for (var i=0; i < rs.rows.length; i++) {
rowOutput += renderTodo(rs.rows.item(i));
}
todoItems.innerHTML = rowOutput;
}
function renderTodo(row) {
return "<li> Weight: " + row.todo + " kg " + "<br />" + " Date: " + row.date + "<br />" + " Note: " + row.note + " [<a href='javascript:void(0);' onclick='curatio.webdb.deleteTodo(" + row.ID +");'>Delete</a>]</li>";
}
function init() {
curatio.webdb.open();
curatio.webdb.createTable();
curatio.webdb.getAllTodoItems(loadTodoItems);
}
function addTodo() {
var todo = document.getElementById("todo");
curatio.webdb.addTodo(todo.value);
todo.value = "";
alert("Your weight has been added");
//window.location = 'users.html'
//location.href="users.html";
}
</script>
... ...
<form type="post" onsubmit="addTodo(); return false;">
<div data-role="fieldcontain">
<label for="todo">
Weight
</label>
<input name="" id="todo" placeholder="75" value="" type="text">
</div>
<div data-role="fieldcontain">
<label for="date">
Date
</label>
<input name="" id="date" placeholder="" value="" type="date">
</div>
<div data-role="fieldcontain">
<label for="note">
Notes
</label>
<input name="" id="note" placeholder="short note"></input>
</div>
<input type="submit" value="Submit" data-inline="true"/>
<a rel="external" data-role="button" data-inline="true" href="users.html">
Cancel
</a>
</form>
答案 0 :(得分:0)
http是无状态的,您必须使用查询字符串将您的参数传递到其他页面 例如 window.location.href =“users.html?myVlue = 9” 然后你在其他页面处理myvalue
我希望这有用
答案 1 :(得分:0)
更改你的onsubmit以返回该函数而不是false ...然后在你的JS函数中只返回false以停止提交表单,或者返回true以正常提交表单。这是我的意思的简短例子
<script>
function validateForm(){
var x=document.forms["myForm"]["fname"].value;
if (x==null || x==""){
alert("First name must be filled out");
return false;
}
}
</script>
<form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">
First name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
答案 2 :(得分:0)
您可以尝试window.opener
将数据从一个页面发送到另一个页面。虽然这将在新窗口中打开页面。有很多页面,请尝试使用Google搜索window.opener
。
如果这对您有用,请告诉我。
答案 3 :(得分:0)
我找到了解决办法! 给DB一两次更新 - 然后重定向 这对我有用:
function addTodo() {
var todo = document.getElementById("todo");
curatio.webdb.addTodo(todo.value);
todo.value = "";
alert("Your weight has been added");
setTimeout(function () {
window.location.href = "users.html"; //will redirect to your blog page (an ex: blog.html)
}, 1000);
}