2014-02-17 94 views
10

我有html形式,当我点击submit按钮页转到insert.php 我不惯于它 preventDefoult不工作 我有这个script HTML的preventDefault不工作,提交按钮

HTML

<form action="insert.php" method="post"> 
<input type="text" name="username" placeholder=""><br /> 
<input type="text" name="name" placeholder=""><br /> 
<input type="text" name="lastname" placeholder=""><br /> 
<input id="mail" name="email" type="text" placeholder="E-mail"><br /> 
<input id="mail_1" type="text" placeholder="reply E-mail"><br /> 
<input id="password" name="password" type="password" placeholder=""><br /> 
<input id="password_1" type="password" placeholder=""><br /> 
<input id="submit" type="submit" value="registration"> 
</form> 

jQuery的

$("#submit").click(function(event){ 
    event.preventDefault(); 
}); 
+0

破碎JS ....... – Rooster

+0

??什么/? @Rooster –

+0

PHP和MySQL几乎没有任何关系(标签编辑,PHP代码已被删除) - 请根据未来的问题进行标记,并且仅发布相关代码。 – CBroe

回答

10

相反的监听按钮,点击你要听的<form>提交:

$("form").submit(function(event){ 
    event.preventDefault(); 
}); 
2

我觉得你只是错过这是取消该事件

$("#target").submit(function(event) { 
    event.preventDefault(); 

});

2

的问题是该事件被冒泡和表单的提交事件被调用。

不是听的click事件的按钮,你应该听的submit事件的形式:

$("#formId").submit(function(event){ 
    event.preventDefault(); 
}); 

而且id属性添加到您的窗体:

<form id="formId" ... 

这应该停止你的工作形式。

+1

无法正常工作:(... –

+0

将'id =“formId”'添加到您的表单 – SparK

+1

仍然无法工作:( –

1

首先给你的表单添加一个ID,我会用​​。

<form id="myFormId" action="insert.php" method="post"> 
    <input type="text" name="username" placeholder=""><br /> 
    <input type="text" name="name" placeholder=""><br /> 
    <input type="text" name="lastname" placeholder=""><br /> 
    <input id="mail" name="email" type="text" placeholder="E-mail"><br /> 
    <input id="mail_1" type="text" placeholder="reply E-mail"><br /> 
    <input id="password" name="password" type="password" placeholder=""><br /> 
    <input id="password_1" type="password" placeholder=""><br /> 
    <input id="submit" type="submit" value="registration"> 
</form> 

然后使用格式ID:

$('#myFormId').on('click', function (event){ 
    event.preventDefault(); 
}); 
+0

请解释我'.on'意思 –

+1

@val https://api.jquery.com/on/- 将其读作“点击”,执行以下操作。 – Dayan