简体   繁体   English

如何在同一页面上通过javascript多次使用相同的表单句柄?

[英]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.

相关问题 Javascript Switch:您可以多次使用相同的案例吗? - Javascript Switch: can you use the same case multiple times? 您如何管理同一页面中多次包含的相同代码的 CSS 选择器? - How can you manage CSS selectors for identical code included multiple times in the same page? 您可以在同一HTML表单上拥有多个Plupload实例吗? - Can you have multiple instances of Plupload on the same HTML form? 关于如何在同一页面上多次调用javascript函数的建议 - Advice on how to make a javascript function callable multiple times on the same page 如何在同一页面上使用相同的javascript变量? - How can I have the same javascript variable on the same page? 在同一页面上多次导入JavaScript文件 - Importing javascript file multiple times on same page 如何在同一页面中多次重复使用JavaScript函数? - How can I reuse a JavaScript function many times in the same page? 当您在同一页面中具有多个具有相同类别的元素时,如何通过一个类别选择ID - How to select an id through a class when you have multiple elements in the same page that have same class 多次处理相同的请求 - Handle same request multiple times 如何在 React 中多次提交相同的表单 - How to submit the same form multiple times in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM