简体   繁体   English

如何通过Ajax POST传递多个具有相同名称的输入数组元素值,例如ex:sikll []

[英]How to pass Multiple input array element values having same name like ex: sikll[] via Ajax POST

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. 有什么方法可以将技能名称传递给服务器而无需序列化,因为我必须在我的search_param对象中添加一个limit属性。 Basically i want all post data in search_param.data and my limit in search_param.limit 基本上我想要search_param.data中的所有发布数据以及search_param.limit中的限制

skill -> array('0'=>'php','1'=>java','2'=>'html') (basically as array in controller) 技能-> array('0'=>'php','1'=> java','2'=>'html')(基本上作为控制器中的数组)

Previously i submitted data as form submit. 以前我以表单提交方式提交数据。 Now i am making this to Ajax. 现在,我正在向Ajax进行此操作。 I tried serialize() to transfer data to server controller via Ajax. 我尝试使用serialize()通过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. 但是问题是我正在使用一个接受极限作为参数的分页函数(例如:param.limit,param是一个对象)。(请在代码下面使用pls 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. 引发Uncaught TypeError:非法调用任何帮助,感激不尽。

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();

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. 变量search_array包含所有输入值。 You can get each values search_array[0], search_array[1] .. etc 您可以获取每个值search_array[0], search_array[1]等。

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);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM