2011-09-15 39 views
19

我试图阻止登录表单提交,所以我可以查询服务器,验证用户名和密码,然后只能移动到主页。event.preventDefault()不会阻止表单提交jQuery Mobile .live()事件

下面是相关代码:

在login.html的:

 <form id="login-form"> 
      <input type="text" name="login-username" id="login-username" value="" placeholder="Username" /> 
      <input type="password" name="login-password" id="login-password" value="" placeholder="Password" /><br /> 
      <button id="cab-submit" type="submit" data-theme="a" data-inline="true">Log In</button> 
     </form> 

在app.js:

$("#login-form").live("submit", function(event) { 
     event.preventDefault(); 
     $.mobile.changePage("main.html", { 
      transition: "fade" 
     }); 
    }); 

我是一个有经验的程序员的jQuery,但从来没有遇到这种。 changePage()确实有效,但只发生在表单提交后导致不必要的页面加载之后。

编辑:找到解决方案。

如果我想自己处理表单提交,我必须在表单中添加data-ajax =“false”。这工作得很好。

新的标记:

 <form id="login-form" data-ajax="false"> 
      <input type="text" name="login-username" id="login-username" value="" placeholder="Username" /> 
      <input type="password" name="login-password" id="login-password" value="" placeholder="Password" /><br /> 
      <button id="cab-submit" type="submit" data-theme="a" data-inline="true">Log In</button> 
     </form> 
+0

什么版本的jQuery Mobile的您使用的是? –

+0

jQuery Mobile 1.0b3。 –

回答

14

回答了我自己的问题。如果我想自己处理表单提交,我必须在表单中添加data-ajax =“false”。这工作得很好。

新的标记:

<form id="login-form" data-ajax="false"> 
     <input type="text" name="login-username" id="login-username" value="" placeholder="Username" /> 
     <input type="password" name="login-password" id="login-password" value="" placeholder="Password" /><br /> 
     <button id="cab-submit" type="submit" data-theme="a" data-inline="true">Log In</button> 
    </form> 
+0

对于任何人在这个问题上磕磕绊绊,在事件处理程序中设置event.preventDefault()应该足以阻止表单提交,而不管表单是否为ajax集合;我正在使用jquery mobile 1.3.1 – Prusprus

+2

是的,它应该是。但它不适合我,我也使用1.3.1。只有在添加data-ajax =“false”之后,它才起作用。 – sp00n

0

尝试改变的提交按钮ID = “提交”,看看会发生什么的ID。它看起来可能是jQuery手机的一个已知问题。

+0

谢谢 - 已经尝试过,没有工作。 –