使用.each()循环选中复选框和.append数据

Chr*_*s J 2 each jquery loops

我在表中有一系列行,每行都有不同的值(价格),每行都有一个复选框.该表是通过PHP循环生成的.

echo "<table border='0' cellpadding='10' id='booking-list'><tr>";
echo "<th>Booking ID</th><th>Customer ID</th><th>Start Date</th><th>Course Name</th><th>Amount Due</th><th>Date Payment Due</th><th>Add to basket</th></tr>";
$x = 0; 
while ($row = mysql_fetch_assoc($query)) {
    $balance = $row['price'] - $row['deposit'];

    echo "<td>" .$row['booking_id']. "</td><td>" .$cust_id. "</td><td>" .$row['start_date']. "</td><td>" .$row['name']. "</td><td>" .$balance. "</td><td>" .$row['balance_due_date']. "</td><td><input type='checkbox' name='' value='" .$x. "' /></td></tr>";
    $x++;
}
echo "</tr><table>";
Run Code Online (Sandbox Code Playgroud)

对于每个选中的复选框,我想将(.append)特定值($balance)添加到form输入字段.附加部分似乎正在工作,但我不知道如何循环遍历jQuery中的每个复选框以获取我需要的值.

function(){
    $('#booking-list input:checkbox').change(
        function(){
            if ($(this).is(':checked')) {
        $('<input />').attr({
                type: 'text',
            id: 'foo',
            name: 'bar',
        value: '<?php echo $balance; ?>'
        }).appendTo('#paypal-form form').text($(this).val());              
            } else {
                $('#paypal-form input:contains('+$(this).val()+')').remove();
            }
        }); 
Run Code Online (Sandbox Code Playgroud)

现在,我只能获得最后$balance生成的值.

谢谢.

sab*_*ker 9

要选中所有选中的复选框,您可以执

  $('#booking-list input:checkbox:checked').each(function(){
      $(this).doSomething();//this is the checked checkbox
  });
Run Code Online (Sandbox Code Playgroud)