简体   繁体   中英

Get multiple values from jquery and post it to php

<input type="checkbox" value="<?= $servicii_content[$j]['title'] ?>" name="check_list" id="check" />

i get multiple values from the table above if the checkboxes are checked with jQuery, like this.

var temp = $("input:checkbox[name=check_list]:checked").map(function(){  
    return $(this).val();
}).get();
alert(temp);

I get the alert with the correct checked values!

I post them later here:

url: "/servicii_email.php",
            type: "POST",
            data: {
                families: temp,

How can I get the elements of "families" eg.:Family1, Family2, Family3, etc. - which are checked to post it to the php file? What is the correct way to do that?

For example, you can just make string with name of variable and it value. That will be something like this Family1=true,Family2=false

On server you just need to split string by , and after that split by = , you will get two dimensional array {{Family1, true}, {Family2, false}}

You can make use of serializeArray() method:

var temp = $("input:checkbox[name=check_list]:checked").serializeArray();
// outputs:  
// [{name:"check_list", value:"theValue"},......n]

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