一个Javascript控件多个表单提交
我正在尝试构建一个包含多个表单的页面,使用Javascript将不同的值提交给另一个php脚本“other script.php”,如下所示:一个Javascript控件多个表单提交,javascript,forms,Javascript,Forms,我正在尝试构建一个包含多个表单的页面,使用Javascript将不同的值提交给另一个php脚本“other script.php”,如下所示: <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> </head> <body>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<div id="simple-msg"></div>
<form name="ajaxform" id="ajaxform" action="another-script.php" method="POST">
<input type=hidden name=a value='value1'>
<input type="button" id="simple-post" value="Run Code" />
</form>
<form name="ajaxform" id="ajaxform" action="another-script.php" method="POST">
<input type=hidden name=a value='value2'>
<input type="button" id="simple-post" value="Run Code" />
</form>
<form name="ajaxform" id="ajaxform" action="another-script.php" method="POST">
<input type=hidden name=a value='value3'>
<input type="button" id="simple-post" value="Run Code" />
</form>
<script>
$(document).ready(function()
{
$("#simple-post").click(function()
{
$("#ajaxform").submit(function(e)
{
$("#simple-msg").html("<img src='/logo/progress_bar.gif'/>");
var postData = $(this).serializeArray();
var formURL = $(this).attr("action");
$.ajax(
{
url : formURL,
type: "POST",
data : postData,
success:function(data, textStatus, jqXHR)
{
$("#simple-msg").html('<pre>'+data+'</pre>');
},
});
e.preventDefault(); //STOP default action
e.unbind();
});
$("#ajaxform").submit(); //SUBMIT FORM
});
});
</script>
</body>
</html>
$(文档).ready(函数()
{
$(“#简单帖子”)。单击(函数()
{
$(“#ajaxform”).submit(函数(e)
{
$(“#简单消息”).html(“”);
var postData=$(this.serializeArray();
var formURL=$(this.attr(“操作”);
$.ajax(
{
url:formURL,
类型:“POST”,
数据:postData,
成功:函数(数据、文本状态、jqXHR)
{
$(“#简单消息”).html(“”+数据+“”);
},
});
e、 preventDefault();//停止默认操作
e、 解除绑定();
});
$(“#ajaxform”).submit();//提交表单
});
});
表单本身应该能够提交不同的值,但是当使用Javascript提交到不同的脚本“other script.php”时,只有第一个表单可以工作。请您告知我应该怎么做,使每个表格提交自己的价值观
多谢各位 考虑到有3个表单具有相同的id,您很可能需要迭代每个表单并在循环中提交。在它们上放置一个类,并对每个类进行迭代<代码>变量形式=$(“.simple post”)代码>
$。每个(表单、函数(表单){/*在此处提交代码*/})代码>ID必须是唯一的,选择器只返回第一个。改用类。另外,您不应该将submit
处理程序绑定到click
处理程序中。它应该装订在顶层。根本不需要单击处理程序——单击submit按钮会触发表单的submit事件。感谢您的推荐。我是Javascript新手。请你修改一下我的代码好让我明白你指的是什么吗?非常感谢。