...

View Full Version : jquery.post: how to make the variable data - [data] - dynamic



lauthiamkok
08-21-2009, 02:47 PM
Hi,
I wonder if i can make the variable data which is [data] in jQuery.post( url, [data], [callback], [type] ) dynamic...

for instance, this is the form i want to send,


<form action="send_xml.php" method="post" enctype="multipart/form-data" id="form_send">
<input type="checkbox" id="var_1" class="checkbox"/>
<input type="checkbox" id="var_2" class="checkbox"/>
</form>

this is the js code,


$("#form_send").submit(function(){
var path = $(this).attr('action');

$(".checkbox").each(function (b,a) {
if(a.id) window[a.id] = a.checked ? 1:"";
});

$.post(path,{
var_1:var_1,
var_2:var_2
},function(xml){
process(xml);
});
return false;
});

I managed to get advice from someone here to loop through the input list in the form with each() so that i dont have to declare variable for each input manually. it was,


if ($('#var_1').attr('checked')) {
var_1 = 1;
}else{
var_1 = '';
}

if ($('#var_2).attr('checked')) {
var_2 = 1;
}else{
var_2 = '';
}

each() has solved the issue perfectly,


$(".checkbox").each(function (b,a) {
if(a.id) window[a.id] = a.checked ? 1:"";
});

but you can see that i still have to declare the data variables manually in the $.post -


var_1:var_1,
var_2:var_2

this is going to be a long list to insert manually, is that anyway to make it dynamic??

many thanks,
Lau



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum