简体   繁体   English

如何使用jQuery Ajax指定正确的输入以提交多种形式(可变数字)

[英]How to specify correct input using jQuery Ajax to submit multiple forms (variable number)

I have a webpage that contains multiple forms. 我有一个包含多种形式的网页。 The forms are dynamically generated and the number of forms varies. 表单是动态生成的,表单的数量也有所不同。

I'm using jQuery AJAX to submit the forms without refreshing the page, but it's not submitting the right information for some of the forms. 我正在使用jQuery AJAX提交表单而不刷新页面,但是它没有为某些表单提交正确的信息。 The problem I have is that the jQuery only seems to pick up the info from the first form on the page. 我的问题是jQuery似乎只从页面上的第一种形式获取信息。 So, if the first form has <input value="sentence A" name="sentence"> , then "sentence A" will be submitted as the sentence for all the forms, even though other forms on the page should have different sentences associated with them. 因此,如果第一个表单的<input value="sentence A" name="sentence"> ,则即使页面上的其他表单应具有不同的句子关联,“句子A”也将作为所有表单的句子提交。跟他们。

I've seen some multi-form jQuery solutions that involve using form ID's, but I think I can't use ID's since the forms are generated and the number varies. 我已经看到了一些涉及使用表单ID的多表单jQuery解决方案,但是我认为我不能使用ID,因为表单是生成的并且数量是变化的。

Any suggestions would be much appreciated! 我们欢迎所有的建议! My jQuery and an example form html are provided below. 下面提供了我的jQuery和一个示例表单html。

I am using the following jQuery to submit the forms on the page (based on this tutorial ) 我正在使用以下jQuery在页面上提交表单(基于本教程

$(document).ready(function() {
$(".button").click(function() {
    var gene1 = $.trim( $("input.gene1").val() );
    var gene2 = $.trim( $("input.gene2").val() );
    var sentence = $.trim( $("input.sentence").val() );
    var suggested_label = $.trim( $("input.suggested_label").val() );

    var dataString = 'gene1='+gene1 + '&gene2=' + gene2 + '&sentence=' + 
    sentence + '&suggested_label=' + suggested_label;

    $.ajax({
        type : "POST",
        url : "/submit_label",
        data : dataString,
        success : function(){
            alert(dataString);
        }
    });

    return false;
});
});

My forms look like the one below. 我的表格如下所示。 On a given page, the form's content for sentence and suggested_label will vary. 在给定的页面上,句子和建议的标签的表单内容将有所不同。

<form action="" name="label_form">
<input value="  5058 " name="gene1" type="hidden" class="gene1">
<input value="  8411 " name="gene2" type="hidden" class="gene2">
<input value="To further demonstrate..." name="sentence" type="hidden" class="sentence">
<div class="textbox">
    <input placeholder="Suggested Label" name="suggested_label" type="text" class="suggested_label">
</div>
<div>
    <button class="button">Submit</button>
</div>
</form>

The problem here is that you have several inputs with the class gene1 , several inputs with the class gene2 , etc... As you mention, when you call $("input.gene1").val() jQuery returns the value of the first matching input with class gene1 . 这里的问题是,您有多个输入class gene1 ,多个输入类gene2 ,等等...正如您提到的,当您调用$("input.gene1").val() jQuery返回的值。 首先input与class gene1匹配。

What you want here is to select the input with class gene1 that is the closest to the button that was clicked. 您要在此处选择类gene1input ,该input与被单击的按钮最接近 You can achieve it by starting from the button and travelling the DOM structure 您可以通过从按钮开始并运行DOM结构来实现

For example, in your code, replace 例如,在您的代码中,替换

var gene1 = $.trim( $("input.gene1").val() );

with

var gene1 = $.trim( $(this).parent().siblings("input.gene1").val() );

and so on for the other inputs 对其他输入依此类推

The suggested_label input needs an extra effort because it is nested: you can use the children() method : describe_label输入是嵌套的,因此需要额外的努力:您可以使用children()方法:

var suggested_label = $.trim($(this).parent().siblings("div").children("input.suggested_label").val());

Note : $(this) represents the clicked button in this context 注意$(this)表示此上下文中的单击按钮

Here is a complete example 这是一个完整的例子

I think conceptually, the solution will be as follows: 我认为从概念上讲,解决方案如下:

  1. Loop through the forms on the page (Using $('form').each( function(){ ... }) ) 循环浏览页面上的表单(使用$('form').each( function(){ ... })
  2. Keep a counter to dynamically assign each form an ID (set a variable like var formNum = 0 and then add to it each iteration in the form loop like: formNum++ ) 保留一个计数器以动态分配每个表单的ID(设置一个变量,如var formNum = 0 ,然后在表单循环中的每个迭代中添加一个变量,例如: formNum++
  3. Then, do .attr('id','ajax_form_' + formNum) 然后,执行.attr('id','ajax_form_' + formNum)
  4. Now, you can refer to each specific form's fields: $('#ajax_form_' + formNum + ' input[name="sentence"])... 现在,您可以引用每个特定表单的字段: $('#ajax_form_' + formNum + ' input[name="sentence"])...

Let me know if you need some more direction as to how to go about this! 让我知道您是否需要更多有关此操作的指导!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM