在AJAX调用中使用FOR循环 [英] Use a FOR loop within an AJAX call
问题描述
因此,我正在尝试发送AJAX请求,但是正如您所看到的,我的表单中有很多字段,并且我使用数组进行验证,我想使用相同的数组,传递要通过AJAX发送的值:
我从来没有在JS中使用过for循环,但是无论如何看起来还是很熟悉.
制作循环的方式,显然是行不通的:
(i = 0; i< required.length; i ++)的 {var required [i] = $('#'+ required [i]).attr('value');
这将创建我想要的变量,如何使用它们?
希望,你们可以帮助我!!!非常感谢你!
required = ['nome','sobrenome','endereco','codigopostal','localidade','telemovel','email','codigopostal2','localidade2','endereco2','nif','entidade','codigopostal3','localidade3','endereco3','nserie','modelo'];函数ajaxrequest(){for(i = 0; i< required.length; i ++){var required [i] = $('#'+ required [i]).attr('value');var dataString ='nome ='+必填[0] +'& sobrenome ='+必填[1];}$ .ajax({输入:"POST",网址:"ajaxload/como.php",数据:dataString,成功:function(){$(.agendarleft").html("SUCESS");}});
为帮助确保传递适当的元素ID和值,请遍历各种元素并将数据首先添加到对象中.
jQuery:
required = ['nome','sobrenome','endereco','codigopostal','localidade','telemovel','email','codigopostal2','localidade2','endereco2','nif","entidade","codigopostal3","localidade3","endereco3","nserie","modelo"]];函数ajaxrequest(){var params = {};//初始化对象//循环遍历输入数组for(var i = 0; i< required.length; i ++){//设置对象的键/属性(输入元素)var ele = required [i];//将属性添加到对象并设置值params [ele] = $('#'+ ele).val();}$ .ajax({输入:"POST",网址:"ajaxload/como.php",数据:参数,成功:function(){$(.agendarleft").html("SUCESS");}});}
演示: http://jsfiddle.net/kPR69/ >
So, what i'm trying to do is to send an AJAX request, but as you can see i have many fields in my form, and i use an array to make validations, i would like to use the same array, to pass the values to be sent via AJAX:
I never used the for loop in JS, but seems familiar anyway.
The way the loop is made, obviously wont work:
for (i=0;i<required.length;i++) {
var required[i] = $('#'+required[i]).attr('value');
This will create the variables i want, how to use them?
HOPEFULLY, you guys can help me!!! Thank you very much!
required = ['nome','sobrenome','endereco','codigopostal','localidade','telemovel','email','codigopostal2','localidade2','endereco2','nif','entidade','codigopostal3','localidade3','endereco3','nserie','modelo'];
function ajaxrequest() {
for (i = 0; i < required.length; i++) {
var required[i] = $('#' + required[i]).attr('value');
var dataString = 'nome=' + required[0] + '&sobrenome=' + required[1];
}
$.ajax({
type: "POST",
url: "ajaxload/como.php",
data: dataString,
success: function() {
$(".agendarleft").html("SUCESS");
}
});
To help ensure that the appropriate element IDs and values are passed, loop through the various elements and add the data to an object first.
jQuery:
required = ['nome', 'sobrenome', 'endereco', 'codigopostal', 'localidade', 'telemovel', 'email', 'codigopostal2', 'localidade2', 'endereco2', 'nif', 'entidade', 'codigopostal3', 'localidade3', 'endereco3', 'nserie', 'modelo'];
function ajaxrequest() {
var params = {}; // initialize object
//loop through input array
for (var i=0; i < required.length; i++) {
// set the key/property (input element) for your object
var ele = required[i];
// add the property to the object and set the value
params[ele] = $('#' + ele).val();
}
$.ajax({
type: "POST",
url: "ajaxload/como.php",
data: params,
success: function() {
$(".agendarleft").html("SUCESS");
}
});
}
Demo: http://jsfiddle.net/kPR69/
这篇关于在AJAX调用中使用FOR循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!