2017-08-12 42 views
0

所以我有jQuery Mobile的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会给我下面的消息:“错误加载页面”(见图)我该怎么办?我需要提交表单,因为我需要表单来验证点击按钮时的字段,这就是为什么我不能只是一个按钮onclick调用一个获取字段值的函数。

任何帮助非常感谢! 在此先感谢!

screenshot

+1

加上'数据的Ajax生成的event =“false”'形成。 – Omar

回答

-1

您可以尝试阻止通过提交form和JS返回false,使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> 
+0

似乎工作!谢谢! –