javascript 在 AJAX 调用中使用 FOR 循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11436347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use a FOR loop within an AJAX call
提问by Souza
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:
所以,我想要做的是发送一个 AJAX 请求,但是正如你所看到的,我的表单中有很多字段,我使用一个数组来进行验证,我想使用相同的数组来传递要通过 AJAX 发送的值:
I never used the for loop in JS, but seems familiar anyway.
我从未在 JS 中使用过 for 循环,但无论如何似乎都很熟悉。
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");
}
});
回答by RobB
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.
为了帮助确保传递适当的元素 ID 和值,请循环遍历各种元素并首先将数据添加到对象中。
jQuery:
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");
}
});
}
回答by Oliver Gray
What would be much cleaner would be to put a class on each of the fields you wish to save and use this to iterate through them. Then you wouldn't need to specify the input names either and you could send a json object directly to the Service;
在您希望保存的每个字段上放置一个类并使用它来遍历它们会更简洁。然后你也不需要指定输入名称,你可以直接向服务发送一个 json 对象;
var obj = {};
$('.save').each(function () {
var key = $(this).attr('id');
var val = $(this).val();
if (typeof (val) == "undefined")
val = "''"
obj[key] = val;
}
Then send obj as the data property of your AJAX call....
然后将 obj 作为您的 AJAX 调用的数据属性发送....
回答by Kalel Wade
There are a few issues with your code. 'required' is being overwritten and is also being re-declared inside of the loop.
您的代码存在一些问题。'required' 正在被覆盖,并且也在循环内重新声明。
I would suggest using pre-written library, a few I included below.
我建议使用预先编写的库,我在下面列出了一些。
http://jquery.malsup.com/form/#validation
http://jquery.malsup.com/form/#validation
https://github.com/posabsolute/jQuery-Validation-Engine
https://github.com/posabsolute/jQuery-Validation-Engine
Otherwise the follow would get you close. You may need to covert the array into a string.
否则跟随会让你接近。您可能需要将数组转换为字符串。
var required = ['nome','sobrenome'];
function ajaxrequest() {
var values;
for (i = 0; i < required.length; i++) {
var values[i] = $('#' + required[i]).attr('value');
}
$.ajax({
type: "POST",
url: "ajaxload/como.php",
data: values,
success: function() {
$(".agendarleft").html("SUCESS");
}
});
}
}