简体   繁体   中英

How can I get successful and not successful controls with jQuery .serialize() for a GET Parameter?

I totally screwd my code and application logic by forgetting that .serialize() only gives me checked elements (checkboxes) of my form. I really need ALL elements because i have to recreate small arrays with the elements in the right order.

Let's say i have 10 checkboxes with the id/names box1 to box10 - now only checkbox 4 and 6 are checked therefore the output is:

...&box4=on&box6=on... but I also need &box1=(whatever just unlike "on" so i can differ) and the other 8 boxes :/.

I shoudld also mention that i have input type textfields and textareas - so it's a mixed thingy. I don't have only checkboxes.

Any ideas?

regards

One solution is to do like this:

<?php

foreach(range(1, 10) as $h)
    echo '<input type="hidden" name="box'.$h.'" value="false" />';

?>

Inserted before your checkboxes. Replace "false".

And if the checkbox is checked, it's value will overwrite the hidden element.

don't use .serialize(). iterate over all the checks and create the query string yourself.

mixed solution:

var query1='', query2=[];
query1=$('#myForm').serialize();
$('#myform :checkbox').not(':checked').each(function(){
    query2.push($(this).attr('name')+'=off');
});
query1+='&'+query2.join('&');

or something like that

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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