preventDefault函数不适用于Ajax/Form Handling
preventDefault function not working with Ajax/Form Handling
下面的表单我一直试图通过Ajax函数而不是通过action/PHP传递。
<section class="registration">
<form name="userRegistration" method="post" id="userregister" action="install.php">
<label for="username">Username</label>
<input type="text" name="username" id="username" value="admin">
<label for="username">Author Name</label>
<input type="text" name="authname" id="authname" placeholder="Example: John Doe">
<label for="password">Password</label>
<input type="password" name="pass" id="pass" value="">
<label for="sitetitle">Site Title</label>
<input type="text" name="sitetitle" id="sitetitle" >
<label for="sitedesc">Site Description</label>
<input type="text" name="sitedesc" id="sitedesc" >
<label for="server">Server</label>
<input type="text" name="server" id="server" placeholder="Example: localhost">
<label for="database">Database</label>
<input type="text" name="database" id="database" >
<label for="dbun">DB Username</label>
<input type="text" name="dbun" id="dbun" >
<label for="dbpw">DB Password</label>
<input type="password" name="dbpw" id="dbpw" >
<input type="submit" />
</form>
下面是我这样做的ajax代码
$(document).on('submit', '#userregister form', function(e) {
$.ajax({
url: $(this).attr('action'),
type: $(this).attr('method'),
data: $(this).serialize(),
success: function(html) {
alert('ok');
}
});
e.preventDefault();
});
但是,表单似乎完全忽略了所有内容,并将其传递到操作中,而不是阻止默认,我不太确定为什么。实际的install.php函数工作正常,但我只是不明白为什么它不通过Ajax。
preventDefault
停止事件冒泡,但取消自身事件。您必须从函数返回false
才能取消默认行为(打开表单url):
$(document).on("submit", "#userregister form", function (e) {
$.ajax({
url: $(this).attr('action'),
type: $(this).attr('method'),
data: $(this).serialize(),
success: function(html) {
alert('ok');
}
});
e.preventDefault();
return false;
});
另外更好的实践是处理sumbit按钮click
,但表单事件"提交"。
相关文章:
- Dojo:访问dijit.form.Select中单击的项目
- Javascript form innerHTML
- 无法在有条件呈现的富:面板上提交h:form
- Javascript form.submit()方法是如何工作的
- 通过jQueryAjax一起发布Array和Form数据时出现问题
- This.Form.Submit() on CakePHP
- 带有Request.Form的JScript数组
- jQuery函数无法处理Form Submit
- 使用application/x-www-form-urlencoded使用node.js在post请求中发送数组
- Javascript:获取“;FORM”;它是在.html到外部.js文件中创建的
- 如何停止向上滚动<按钮dojoType=“;dijit.form.Button”;type=“;提交”/>在
- Angular Form Wide验证(不特定于字段)
- 当在coldfusion中通过AJAX传递时,FORM提交在IE浏览器中给出空表单
- Form获胜'没有ID主联系表格无法验证
- 如何使用SpringWebFlow在JavaScript中处理form.submit()
- Form.target=window.opener.name不起作用
- 在Javascript中向Ext.form.Formpanel添加多个按钮
- Javascript innerHTML with form
- this.form.submit()无法正常使用php
- preventDefault函数不适用于Ajax/Form Handling