i wanted create dynamic array user defined key , value. have tried best couldn't find solution.
this have tried:
function demo1(form_no) { var post_data_array = []; var post_data_array2 = []; var array1 = ['abc', 'xyz']; $.each(array1, function(key, value) { var temp_value = $("#" + value + "_" + form_no).val(); var temp_key = value + "_" + form_no; post_data_array.push({ key2: temp_key, value2: temp_value }); }); $.each(post_data_array, function(index, value) { post_data_array2[value.key2] = value.value2; }); .ajax({ type: "post", data: { postdata121: post_data_array2 }, url: base_url + "/temp_function/" + form_no, success: function(result) { alert(result); } }); }
but after assigning values post_data_array2
getting empty array if alert post_data_array2
.
and try post post_data_array2
in ajax function post data in page, getting postdata empty.
i don't know what's happening.
it seems though you're saying want use values in array1
keys. that, instead of array []
, use object {}
.
this can accomplished less code too:
function demo1(form_no) { var array1 = ['abc', 'xyz']; var data = {}; $.each(array1, function(key, value) { var temp_key = value + "_" + form_no; data[temp_key] = $("#" + temp_key).val(); }); alert(json.stringify(data)); $.ajax({ type: "post", data: { postdata121: data }, url: base_url + "/temp_function/" + form_no, success: function(result) { alert(result); } }); } demo1(123);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <input id=abc_123 value=abcvalue> <input id=xyz_123 value=xyzvalue>
now when loop on array1
, use each member create new key/value pair in data
object, gets sent via $.ajax
.
Comments
Post a Comment