[英]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. 用名字,您将所有值。
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 : 最理想的方法是:
id="ajaxForm"
) id="ajaxForm"
) 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.