jQuery 没有操作的移动表单给出错误消息:"error loading page"

jQuery Mobile form without action gives error message: "error loading page"

所以我有一个 HTML 文档,其中包含 jQuery Mobile 和一个表单元素,如下所示:

<head>

<!-- Include jQuery Mobile stylesheets -->
<link rel="stylesheet" href="lib/jquery.mobile-1.4.5.min.css">
<!-- Include the jQuery library -->
<script type="text/javascript" src="lib/jquery-1.11.3.min.js"></script>
<!-- Include the jQuery Mobile library -->
<script type="text/javascript" src="lib/jquery.mobile-1.4.5.min.js">
</script>

</head>

<body>

<form onsubmit="myFunction()" id="myForm">

    <input type="text">

</form>

<button type="submit" form="myForm">Submit</button>

<script>
    function myFunction() {
        //does somethign with form values
    }

</script>

</body>

提交表单时,我希望 JavaScript 处理字段值(通过 onsubmit 调用的函数)。我不想将它发送到另一个文档。那就是为什么我遗漏了 action 属性。但是,当我点击提交时,jQuery Mobile 给我以下消息:"error loading page"(见图片)。我能做什么?我需要要提交的表单,因为单击按钮时我需要表单来验证字段。这就是为什么我不能只制作一个 onclick 调用获取字段值的函数的按钮。

非常感谢任何帮助!! 提前致谢!

您可以尝试通过提交 form 并从 js 返回 false 来阻止生成 event,这样 js 函数被调用但 form 不会被调用已提交

<form onsubmit="myFunction(event)" id="myForm">
                    <!--   ^^^^^ -> pass the event -->
    <input type="text">

</form>

<button type="submit" form="myForm">Submit</button>

<script>
    function myFunction(e) {
                      //^ get the event so we can prevent
        e.preventDefault();

        //does somethign with form values

        return false;
    }

</script>