admin管理员组

文章数量:1302382

I have 4 checkboxes. All in form of;

input type="checkbox" name="Group[]" id="Group" value="uniquevalue"

I have 3 of 4 of them checked. How can I send the values that are checked in the form

"a = chkedvalue1 &b = chkedvalue2 &c = chkedvalue3"?

I'm using jquery 1.4.2

Thank you! Spent a whole day with this issue to no avail.

I have 4 checkboxes. All in form of;

input type="checkbox" name="Group[]" id="Group" value="uniquevalue"

I have 3 of 4 of them checked. How can I send the values that are checked in the form

"a = chkedvalue1 &b = chkedvalue2 &c = chkedvalue3"?

I'm using jquery 1.4.2

Thank you! Spent a whole day with this issue to no avail.

Share edited Dec 23, 2010 at 11:53 Haim Evgi 126k46 gold badges222 silver badges226 bronze badges asked Dec 23, 2010 at 11:51 DariusDarius 111 silver badge2 bronze badges 1
  • 1 Don't. Really. Have the server side process expect Group[]=chkedvalue1&Group[]=chkedvalue2&Group[]=chkedvalue3 (which is how the form data will be encoded using a normal forum submission (this way you can build on things that work)) and then just use jQuery's serialize() – Quentin Commented Dec 23, 2010 at 11:54
Add a ment  | 

3 Answers 3

Reset to default 4

First, remove that id attribute, it needs to be unique. Then to get the array of values you can just .serialize() the <form> like this:

var data = $("form").serialize();

Note this will produce Group[]=..., remove the [] from the name attributes if you just want Group= for each.

...or if you want the array of values, use .map(), like this:

var arr = $("input[name='Group[]']:checked").map(function() { 
            return this.value; 
          }).get();
<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("test",$con);
$ID = $_GET['id'];
$sql = "select city_name from city where id='$id'";
$fetch = mysql_fetch_array(mysql_query($sql));
$result = explode(",",$fetch['city_name']);
?>
<html>
<body>
<form method="post" >
<input type="checkbox" name="city[]" value="Faridabad" <?php for($i=0;$i<4;$i++) if($result[$i]=='Faridabad')echo 'checked'; ?> />Faridabad
<input type="checkbox" name="city[]" value="Gurgaon" <?php for($i=0;$i<4;$i++) `if($result[$i]=='Gurgaon') echo 'checked';?>/>Gurgaon`
<input type="checkbox" name="city[]" value="Noida"<?php for($i=0;$i<4;$i++) if($result[$i]=='Noida') echo 'checked'; ?>/>Noida
<input type="checkbox" name="city[]" value="Gaziabad" <?php for($i=0;$i<4;$i++) if($result[$i]=='Gaziabad') echo 'checked'; ?>/>Gaziabad
</form>
</body>
</html>

I suggest you serialize the form.

本文标签: phpHow do I get multiple checkbox values and send the values via ajax in jqueryStack Overflow