如何使用jQuery获取复选框的值并将其分配给文本框的值?

时间:2019-04-29 20:21:31

标签: php jquery html mysql

我正在尝试制作带有列(复选框,商品,数量,价格)的餐厅订单表(窗体)。 我想获取复选框的值并将其分配给文本框的值,因为知道该表是使用PHP代码从Mysql数据库中填充的。 我想在每次单击相应复选框时在相应的文本框中显示每个项目的价格。

//我尝试使用jqyery函数

$(document).ready(function(){
  $('input:checkbox').click(function(){
    $('input:checkbox:checked').each(function(){
     var price = parseInt(($(this).val()));
    });
    $('#<?php echo $row['idchoix'] ?>').val(price);
  });
});

// the php code for the table

<?php

// Check connection

$result = mysqli_query($con,"SELECT * from menu order by type");

echo "<form action='order.php' method='POST' >";
echo "<legend id='legend'>Nouvelle commande</legend>";
echo "<table class='table table-bordered table-striped table-hover' >
<tr>
<th>Categorie</th>
<th></th>
<th>Article</th>
<th>Prix unitaire</th>
<th>Quantité</th>
<th>Somme</th>
</tr>";



while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['type'] . "</td>";

echo "<td> <input type='checkbox' class='form-check-input' id='check1' value='".$row['prixunitaire']."'> </td>";
echo "<td>" . $row['choix'] . "</td>";

echo "<td>" . $row['prixunitaire'], "Dh" . "</td>";

echo "<td> <div class='btn-toolbar' role='toolbar' aria-label='Toolbar with button groups'>
  <div class='btn-group mr-2' role='group' aria-label='First group' required='required'>
    <button type='button' class='btn btn-secondary' >x1</button>
    <button type='button' class='btn btn-secondary'>x2</button>
    <button type='button' class='btn btn-secondary'>x3</button>
    <input type='number' class='btn btn-secondary form-control' style='width:70px;' name='quantity' value='4' ></div></div></td>";


echo "<td> . <input type='number' class='btn btn-secondary form-control' style='width:200px;' id='".$row['idchoix']."' name='quantity' value=00.00 readonly> </td>";


echo "</tr>";
}

echo "</table>";

echo "<input class='btn btn-success form-control' type='submit' value='Envoyer la commande'>";

echo "</form>";
?>

1 个答案:

答案 0 :(得分:0)

您可以尝试这样,我认为这是价格变量的范围问题

$(document).ready(function(){
  $('input:checkbox').click(function(){
    var price;
    $('input:checkbox:checked').each(function(){
     price = parseInt(($(this).val()));
    });
    $('#<?php echo $row['idchoix'] ?>').val(price);
  });
});
相关问题