开发者

how to get value of checked element in for loop

I am working to get each value of the checked element and post them to php. But it only gets first value of just one checked item.

here is

$("#conf").click(function(){
    var count = $("input:checked").length;
    for(i = 0; i < count; i++) {
         a = $("input:checked").val();
     $.post("reqs.php?act=confirm", { ID: a }, function(data) { });
     $('#'+a).parents开发者_如何学Go(".req")
         .animate({ backgroundColor: "#fbc7c7" }, "fast")
         .animate({ opacity: "hide" }, "slow");
    }
});

And HTML

<?php while ($info = mysql_fetch_assoc($result)) { ?>
<tr class="req">
    <td style="width: 29px">
        <input name="confirm" type="checkbox" id="<?php echo $info['ID']; ?>" value="<?php echo $info['ID']; ?>" />
    </td>
    <td style="width: 70px" class="style5"><?php echo $info['email']; ?></td>
    <td style="width: 72px" class="style5"><?php echo $info['name']; ?></td>
    <td style="width: 88px" class="style5"><?php echo $info['username']; ?></td>
    <td style="width: 76px" class="style5"><?php echo $info['country']; ?></td>
    <td style="width: 76px" class="style5"><?php echo $info['bus']; ?></td>
    <td style="width: 67px" class="style5"><?php echo $info['website']; ?></td>
    <td style="width: 97px" class="style5"><?php echo $info['music']; ?></td>
    <td style="width: 78px" class="style5"><?php echo $info['radio']; ?></td>
</tr>
<?php } ?>

How can i get value of checked items in that for loop ?

Thanks


Change

a = $("input:checked").val();

to

a = $("input:checked").eq(i).val();

This will get you the value for each subsequent item.

And use Firebug! :)


$("input:checked").each(function() {
   alert($(this).val());
});


You should iterate over all the checked elements, you can use $.each, then add the results to an object and post that object to the server:

$("#conf").click(function(){
  var data = {};
  $('input:checked').each(function () {
    var el = $(this);
    data[el.attr('id')] = el.val();
  });

  $.post("reqs.php?act=confirm", data, function(response) { });
});

The data object will be formed by Key/Value pairs containing the ID/Value of the checked elements.


Even better, let the magic of jQuery work for you:

$("#conf").click(function(){
  $("input:checked").each(){
    var a = $(this).val();
    $.post("reqs.php?act=confirm", { ID: a }, function(data) { });
    $('#'+a).parents(".req")
      .animate({ backgroundColor: "#fbc7c7" }, "fast")
      .animate({ opacity: "hide" }, "slow");
  });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜