0

提交详细信息后,我正在尝试重定向到其他页面。我已经尝试了注释掉的代码。它确实会重定向,但不会接收数据。当我将其注释掉时,它不会重定向,但会接收数据。我希望我能正确解释这一点。

有任何想法吗?

      <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>&nbsp;&nbsp;&nbsp;&nbsp;Weight:&nbsp;" + row.todo  + "&nbsp;kg&nbsp;" + "<br />" + "&nbsp;&nbsp;&nbsp;&nbsp;Date:&nbsp;" + row.date + "<br />" + "&nbsp;&nbsp;&nbsp;&nbsp;Note:&nbsp;" + row.note + "&nbsp;&nbsp;&nbsp;&nbsp;[<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>
4

4 回答 4

0

http 是无状态的,您必须使用查询字符串将参数传递到其他页面,例如 window.location.href = "users.html?myVlue=9" 然后在其他页面中处理 myvalue

我希望这有帮助

于 2013-10-31T12:52:29.243 回答
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>
于 2013-10-31T12:54:29.433 回答
0

您可以尝试window.opener将数据从一页发送到另一页。虽然这会在新窗口中打开页面。这方面有很多页面,请尝试谷歌搜索window.opener。如果这对你有用,请告诉我。

于 2013-10-31T12:54:37.100 回答
0

我找到了解决方法!给数据库一两秒钟更新 - 然后重定向这对我有用:

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);
}
于 2013-10-31T13:56:32.807 回答