[英]how can you have the same form handle by javascript multiple times on the same page?
I have a thumb gallery where I am using ajax/javascript to submit a form per image to report the image as broken seamlessly along with php. 我有一个拇指画廊,我在这里使用ajax / javascript为每个图像提交一个表单,以报告该图像与php无缝地断开。 The form and script is templated so the script is in the header and then the form is printed multiple times on the same page with a hidden field with a different id for the value per thumb.
表单和脚本是模板化的,因此脚本位于页眉中,然后表单在同一页面上多次打印,并带有一个隐藏字段,每个字段的值具有不同的ID。 So basically this is what i have.
所以基本上这就是我所拥有的。
javascript in header 标头中的javascript
just a quick idea of the forms i have. 只是对我拥有的表格有个简单的了解。 Just a quick idea not what I actually have.
只是一个快速的想法,而不是我实际拥有的。
image1 followed by the form image1后跟表格
image2 followed by the form image2后跟表格
So when you hit the button it basically submits all of the forms at the same time. 因此,当您按下按钮时,它基本上同时提交了所有表单。 I am sure it can be fixed with a (this) or something like that so it only submits a single form at a time.
我确信可以用(this)或类似的东西来修复它,因此它一次只能提交一个表格。 Let me know please.
请让我知道。
$(function() { $(function(){
$(".submit").click(function() { $(“。submit”)。click(function(){
var imgId = $("#imgId").val(); var imgId = $(“#imgId”)。val();
var dataString = 'imgId='+ imgId; var dataString ='imgId ='+ imgId;
if(imgId==''){ 如果(imgId ==''){
$('.success').fadeOut(200).hide(); $('。success')。fadeOut(200).hide();
$('.error').fadeIn(200).show(); $('。error')。fadeIn(200).show();
$('.error').fadeOut(200).hide(); $('。error')。fadeOut(200).hide();
}else{ }其他{
$.ajax({ $ .ajax({
type: "POST", 输入:“ POST”,
url: "inc/brokenImgReport.php", 网址:“ inc / brokenImgReport.php”,
data: dataString, 数据:dataString,
success: function(){ 成功:function(){
}); });
$('.error').fadeOut(200).hide();
$('.success').fadeIn(200).show();
setTimeout(function() {
$('.success').fadeOut(200); }, 2000);
}
return false;
});
}); });
in a page with multiple forms. 在具有多种形式的页面中。 You can refer individual form as an array element forms[0],forms[1] and likewise, on click of submit button.
您可以将单个表单称为数组元素forms [0],forms [1],同样,单击“提交”按钮。 you can do
你可以做
forms[0].submit 表格[0]。提交
forms[1].submit 表格[1]。提交
and likewise 同样
$(document).ready( function(){
$('form.your_class_if_needed').submit( function(){
do_your_stuff_here()
})
})
Every form with class your_class_if_needed
will have the function you define attached to its submit
method. 用类各种形式
your_class_if_needed
将你定义附加到其功能submit
方法。 Within that function, $(this)
will refer to each specific form -- not all of them. 在该函数中,
$(this)
将引用每种特定形式-并非全部。 You can then easily select, process, and assemble your form fields and values within the context of each form, and submit them with $.ajax()
or $.post()
. 然后,您可以轻松地在每个表单的上下文中选择,处理和组合表单字段和值,并使用
$.ajax()
或$.post()
提交它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.