javascript 如何通过 Ajax POST 传递具有相同名称的多个输入数组元素值,例如:sikll[]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22400490/
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
How to pass Multiple input array element values having same name like ex: sikll[] via Ajax POST
提问by George Joffin Joy
I have a form with 4 skill inputs having html like:
我有一个包含 4 个技能输入的表单,其 html 如下所示:
<input type="text" id="skill1" name="skill[]" value="">
<input type="text" id="skill2" name="skill[]" value="">
<input type="text" id="skill3" name="skill[]" value="">
<input type="text" id="skill4" name="skill[]" value="">
I need to get this in controller like:
我需要在控制器中获取它,例如:
Is there any way to get skill names passed to server without serialize as i have to append a limit attribute to my search_param which is an object. Basically i want all post data in search_param.data and my limit in search_param.limit
有什么方法可以在不序列化的情况下将技能名称传递给服务器,因为我必须将限制属性附加到我的 search_param 对象。基本上我想要 search_param.data 中的所有发布数据和 search_param.limit 中的限制
skill -> array('0'=>'php','1'=>java','2'=>'html') (basically as array in controller)
Skill -> array('0'=>'php','1'=>java','2'=>'html')(基本上作为控制器中的数组)
Previously i submitted data as form submit. Now i am making this to Ajax. I tried serialize() to transfer data to server controller via Ajax. But issue is that i am using a paginate function which accept limit as parameter(ex: param.limit, param is an Object).(pls chk below code) I need to pass both data and limit to ajax paginate function.
以前我提交数据作为表单提交。现在我正在向 Ajax 制作这个。我尝试 serialize() 通过 Ajax 将数据传输到服务器控制器。但问题是我正在使用一个分页函数,它接受限制作为参数(例如:param.limit,param 是一个对象)。(请在代码下面 chk)我需要将数据和限制传递给 ajax 分页函数。
I need to pass both post data and limit to paginate function.
我需要将发布数据和限制传递给分页功能。
Below is the code:
下面是代码:
function getUsers() {
var search_param = {'data':jQuery('#candidateForm').serialize()};
jQuery.ajax({
type: "POST",
dataType:'JSON',
url: jQuery('#site').val() + "search/ajax_search",
data: search_param,
cache: false,
success: function(data) {
if (data) {
//something
}else{
//else something
}
search_param.limit = 14; //this is desired way but as serialized it wont work
paginate_4a(jQuery('#site').val()+'search/ajax_search', 'srchResultCnt', search_param, {'fn':'fn_name', 'index':0});
}
});
}
TRIED THIS BUT FAILED:
试过这个但失败了:
var skill = jQuery('input[name="skill[]"]').map(function(){return jQuery(this).val();});
var skill_hid = jQuery('input[name="skill_hid[]"]').map(function(){return jQuery(this).val();});
var experience = jQuery('input[name="experience[]"]').map(function(){return jQuery(this).val();});
var search_param = {
'skill':skill,
'skill_hid':skill_hid,
'experience':experience
};
Throwing Uncaught TypeError: Illegal invocation Any help is appreciated.
抛出未捕获的类型错误:非法调用任何帮助表示赞赏。
SOLUTION:
解决方案:
use get() at last and now getting array in server side...thanks to Nisam....
最后使用 get() ,现在在服务器端获取数组...感谢 Nisam....
$('input[name="skill[]"]').map(function(){return $(this).val();}).get();
$('input[name="skill[]"]').map(function(){return $(this).val();}).get();
回答by Nisam
data param should be,
数据参数应该是,
data: {searchaparams:search_param,page:1}
You can get the input values as array like,
您可以像数组一样获取输入值,
var search_array = $('input[name="skill[]"]').map(function(){return $(this).val();}).get();
The variable search_array
contains all the input values.
You can get each values search_array[0], search_array[1]
.. etc
该变量search_array
包含所有输入值。你可以得到每个值search_array[0], search_array[1]
..等
回答by Mathias
Try this: http://jsfiddle.net/borglinm/p8hY7/
试试这个:http: //jsfiddle.net/borglinm/p8hY7/
You iterate over all the inputs and build an array
您遍历所有输入并构建一个数组
var inputs = [];
$("input").each(function(index, elem) {
inputs.push(elem.value);
});
var postData = {
'data': inputs,
'otherParam': 1
}
console.log(postData);