简体   繁体   English

如何通过Ajax发送具有相同类名或ID的多个输入值

[英]How to send multiple input values with same class name or id via ajax

I'm trying to send multiple input values via AJAX to my PHP script. 我正在尝试通过AJAX将多个输入值发送到我的PHP脚本。 It's working fine when I use getElementById . 当我使用getElementById时,它工作正常。 However I have the option to add a child. 但是,我可以选择添加一个孩子。 It iterates the input fields, then I get values only from the first child. 迭代输入字段,然后我仅从第一个孩子获得值。 I tried to use getElementsByClassName but it gives values as undefined . 我尝试使用getElementsByClassName但它给出的值是undefined This is my code: 这是我的代码:

<div id="name-field" class="name-field row">
  <div class="col-xs-12 col-sm-6 childname">
    <div class="field text-left">
      <label class="text-left">Name of child</label>
      <input id="firstname" class="firstname" name="firstname" type="text" />
    </div>
  </div>
  <div class="col-xs-12 col-sm-6 dateofbirth">
    <div class="field text-left">
      <label class="text-left">Date of birth</label>
      <input type="text" class="date" id="thedate" />
    </div>
  </div>
</div>

<a href="#" id="addChild" name="addchild" class="btn-success">Add Child</a>

<a href="#" id="stepname" class="btn" onclick="btnSubmit('step1')">Next Step</a>
//Iterate child function
jQuery(function($) {
  $("#addChild").click(function() {
    $(".name-field:first").clone().find("input").val("").end()
      .removeAttr("id")
      .appendTo("#additionalselects")
      .append($('<a class="delete" href="#"><i class="fa fa-times"></i></a>'));
  });
  $("body").on('click', ".delete", function() {
    $(this).closest(".name-field").remove();
  });
});

//Sending values function
function btnSubmit(step) {
  //Set Var with Name
  //Set Var with DoB
  if (step == 'step1') {

    //values using ID
    var Name = document.getElementById("firstname").value;
    var DoB = document.getElementById("thedate").value;

    //Values using classname
    var Name = document.getElementsByClassName("firstname").value;
    var DoB = document.getElementsByClassName("date").value;

    //Create a Variable catVar Having the Var Name and Var DoB Concatinated with a --

    var stepVar = Name + "--" + DoB;

    $(".thevoornaam, .date").each(function() {
      alert();
    });
  } else {

  }
  var xmlhttp;
  if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
  } else { // code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {



    }
  }
  xmlhttp.open("GET", "validations/btnSubmit.php?q=" + step + "&q2=" + stepVar, true);
  xmlhttp.send();
}

I hope you guys understand what I'm trying to achieve with this, if I did't explain it correctly. 如果我没有正确解释的话,我希望你们能理解我要达到的目标。

Thanks in advance. 提前致谢。

using jQuery you can do it in this way. 使用jQuery,您可以通过这种方式进行操作。

var firstname = [];
$("#name-field .firstname").each(function(){
 firstname.push($(this).val());
});

In firstname you will all the values. 用名字,您将所有值。

Update 更新资料

Here is a working pen. 这是一支工作笔。

https://codepen.io/smitraval27/pen/dmvwVB https://codepen.io/smitraval27/pen/dmvwVB

document.getElementsByClassName("firstname")

returns an array... so the following will give you the elements value with index i. 返回一个数组...因此下面将为您提供带有索引i的元素值。 To get all the values you will need to do a ForEach. 要获取所有值,您需要执行一次ForEach。

document.getElementsByClassName("firstname")[0].value

Since you're using JQuery elsewhere, why not use... 由于您在其他地方使用JQuery,为什么不使用...

$('.firstname')[0].val();

The most ideal way to do this is : 最理想的方法是:

  • add a form wrapper to your fields ( id="ajaxForm" ) 将表单包装器添加到您的字段( id="ajaxForm"
  • to have a name to all your form fields and use the below code to generate the data to be passed to your AJAX call 有一个name你的所有表单域,并使用下面的代码来生成data将被传递到您的AJAX调用

$("#ajaxForm").serialize()

The return value of $.serialize() can be directly used as the data for the ajax call $.serialize()的返回值可以直接用作ajax调用的data

 $("#ajaxForm").on("submit", function () { var ajaxData = $("#ajaxForm").serialize(); $("#ajaxData").text(ajaxData); $.ajax({ url:"", type: "get", data : ajaxData, success: function (resp) {} }); console.log(ajaxData); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="ajaxForm" onsubmit="return false" > <input id="firstname" class="firstname" name="firstname" type="text" /> <input type="text" name="date" class="date" id="date" /> <input type="submit" value="submit"> </form> <div id="ajaxData"></div> 

暂无
暂无

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

相关问题 通过Ajax发送相同名称的多个复选框值 - Send same name multiple checkbox values via ajax 如何通过Ajax POST传递多个具有相同名称的输入数组元素值,例如ex:sikll [] - How to pass Multiple input array element values having same name like ex: sikll[] via Ajax POST 如何通过Ajax在php上发送多个已创建输入字段的值 - How to send the values of multiple created input fields on php via ajax 如何使用ajax和php发送多个同名输入数据 - How to send data multiple same name input using ajax and php 从具有相同类的输入字段中获取所有值,然后通过AJAX调用发送到脚本 - Obtain All Values from Input Fields Having Same Class and Send to a Script via an AJAX Call 如何将多个具有相同类名的id值作为数组发送到Codeigniter中的数据库? - How to send multiple id values with same classname to ajax as an array to the databse in codeigniter? 如何获得具有相同ID的多个输入值 - how to get multiple input values with same id 如何将具有相同类和名称的多个输入字段传递给ajax请求并根据回复更改相应的字段? - How to pass multiple input fields having same class and name to ajax request and change corresponding fields based on reply? 如何使用jquery ajax发送多个输入字段值的对象 - How to send an object of multiple input field values with jquery ajax 如何通过 ajax 发布多个附加的输入字段值 - How to post multiple appended input field values via ajax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM