dsai1991 2019-04-29 20:21
浏览 233

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

I am trying to make a restaurant order table(form) with the columns (checkbox, article, quantity, price). I would like to get the value of the checkbox and assign it to the value of the textbox, knowing that the table is populated from the Mysql database using a PHP code. I want to make the price of each item visible in the corresponding textbox, everytime I click on its corresponding checkbox.

// my attempt at a jqyery function

$(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条回答 默认 最新

  • dtio35880438 2019-04-30 09:16
    关注

    You can try like this I think it is a scope issue for price variable

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

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?