通过jquery ajax()和serialize()提交html表单
submitting html form via jquery ajax() and serialize()
我想通过jquery ajax提交这个表单,这是我做的,但它不起作用。即表单正在提交页面刷新,而我没有看到响应,即在同一页面上打印数组。
HTML
<link rel='stylesheet' type='text/css' href='css/pepper-grinder/jquery-ui-1.10.4.custom.css' />
<script type='text/javascript' src='js/jquery-1.10.2.js' ></script>
<script type='text/javascript' src='js/jquery-ui-1.10.4.custom.min.js' ></script>
<form id="form1" method="get" action="submit.php ">
<label>Name of Organization</label>
<input type="text" name="OrgName" id="OrgName" class="textfield">
<label>Address of Organization</label>
<input type="text" name="OrgAddress" id="OrgAddress" class="textfield">
<input type="submit" value="Register Organization">
</form>
<div id="response">ads</div>
<script>
$document.ready(function(){
$("#form1").click((function(event){
event.preventDefault();
$.ajax({
url:'submit.php',
type:'GET',
data:$(this).serialize(),
success:function(result){
$("#response").text(result);
}
});
});
});
</script>
PHP(submit.PHP)
<?php
print_r($_GET);
?>
使用这个-出现了一些语法错误,必须提交事件
$(function(){
$("#form1").submit(function(event){
event.preventDefault();
$.ajax({
url:'submit.php',
type:'GET',
data:$(this).serialize(),
success:function(result){
$("#response").text(result);
}
});
});
});
$document.ready(function(){
$("#form1 input[type='submit']").click(function(event){
event.preventDefault();
$.ajax({
url:'submit.php',
type:'GET',
data:$(this).serialize(),
success:function(result){
$("#response").text(result);
}
});
});
});
我是你的代码$("#form1").click(….在这里没有任何意义…你需要在按下提交按钮时使用事件处理程序。所以我认为如果你选择合适的选择器,那么它可能会完美地工作
$("#form1").click((function(event){
更改为
$("#form1").submit((function(event){
函数$(document).ready(function()
中的错误
试试这个
$(document).ready(function(){
$("#form1").submit(function(event){
event.preventDefault();
$.ajax({
url:'submit.php',
type:'GET',
data:$(this).serialize(),
success:function(result){
$("#response").text(result);
}
});
});
});
2条备注:
submit函数需要返回false才能停止正常发布你可以在表单中使用onsubmit属性,而不是像这个那样使用ready等
<form onsubmit="$.ajax({url:'submit.php', type:'GET',data:$(this).serialize(), success:function(result){$("#response").text(result);});return false;" id="form1" method="get" action>
相关文章:
- 如何在提交html表单后保持相同的位置
- 使用 WebView for Android 提交 HTML 表单
- 纯javascript的进度图像加载程序仅当用户单击提交html表单时
- 为什么这个javascript/jQuery没有提交html表单
- 提交HTML表单,该表单使用带有cURL的javascript进程
- 在 javascript 中提交 HTML 表单上的多个输入值
- 异步数据加载后提交HTML表单的正确方法是什么
- 通过 AJAX 请求提交 HTML 表单
- 在提交 HTML 表单后从数组中提取数据(按键事件)
- SilverStripe 通过 Ajax 提交 HTML 表单
- 我可以使用 Ajax 和 Visual Composer (Raw HTML 和 Raw JS) 提交 HTML Sal
- 尝试通过javascript提交html表单变量时没有任何反应
- Iframe 发布表单 - 在更改输入字段时提交 html 表单
- 在提交 HTML 表单后保留 JavaScript 中的 innerHTML 值
- 用于在离线时提交 HTML 表单的 Javascript
- 无法在 Angular 2 应用程序中提交 HTML 表单
- 提交 HTML 表单而不打开新窗口
- 在php中提交html表单
- 如何在提交HTML表单时强制不接收数据
- 如何在不重定向的情况下提交 HTML 表单