为了账号安全,请及时绑定邮箱和手机立即绑定

如何在多行上使用 JavaScript 填充下拉菜单

如何在多行上使用 JavaScript 填充下拉菜单

PHP
梦里花落0921 2022-10-28 15:25:30
我正在尝试来自https://www.phpzag.com/build-invoice-system-with-php-mysql/的发票生成系统。在https://phpzag.com/demo/build-invoice-system-with-php-mysql-demo/create_invoice.php上进行演示。一切正常,但作为示例给出的字段只是输入字段。但是,我需要使用我的 mysql 数据库中的选择选项。“htmlRows”中给出的字段应该在用户想要使用 html 表单中的添加按钮时添加。我创建了一个单独的函数来从数据库中提取产品,现在我不知道为什么它们没有被填充到“htmlRows”的选项值中。       <script type="text/javascript">             $(document).ready(function(){                $(document).on('click', '#checkAll', function() {                               $(".itemRow").prop("checked", this.checked);                });                 $(document).on('click', '.itemRow', function() {                        if ($('.itemRow:checked').length == $('.itemRow').length) {                        $('#checkAll').prop('checked', true);                    } else {                        $('#checkAll').prop('checked', false);                    }                });                  var count = $(".itemRow").length;                $(document).on('click', '#addRows', function() {                     count++;                  //Load products                  $.getJSON("load.php?task=products",{ajax: 'true'}, function(j){                  var options = '<option value="">--------------------------- select -------------------------</option>';                 count++;                      for (var i = 0; i < j.length; i++) {                                options += '<option value="' + j[i].id + '">' + j[i].display + '</option>';                      }                      $("select#productCode_'"+count+"'").html(options);                  });                     var htmlRows = '';                    htmlRows += '<tr>';                    htmlRows += '<td><input class="itemRow" type="checkbox"></td>';                });             </script>   
查看完整描述

1 回答

?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

将您的 javascript 代码替换为以下内容:


<script type="text/javascript">

$(document).ready(function(){


    $(document).on('click', '#checkAll', function() {           

        $(".itemRow").prop("checked", this.checked);

    }); 


    $(document).on('click', '.itemRow', function() {    

        if ($('.itemRow:checked').length == $('.itemRow').length) {

            $('#checkAll').prop('checked', true);

        } else {

            $('#checkAll').prop('checked', false);

        }

    });  


    var count = $(".itemRow").length;


    $(document).on('click', '#addRows', function(options) {

        count++;

        //Load products

        $.getJSON("load.php?task=products",{id: $(this).val(), ajax: 'true'}, function(j){

            count++;

            var options = '<option value="">--------------------------- select -------------------------</option>';

            for (var i = 0; i < j.length; i++) {

                options += '<option value="' + j[i].id + '">' + j[i].display + '</option>';

            }

            console.log(options);

            var htmlRows = '';

            htmlRows += '<tr>';

            htmlRows += '<td><input class="itemRow" type="checkbox"></td>';


            //This should be a drop down menu


            htmlRows += '<td><select name="productCode[]" id="productCode_'+count+'" class="form-control" autocomplete="off" style="width:450px;font-weight:bold;">'+options+'</select></td>';           

            htmlRows += '<td><input type="number" name="quantity[]" id="quantity_'+count+'" class="form-control quantity" autocomplete="off"></td>';        

            htmlRows += '<td><input type="number" name="price[]" id="price_'+count+'" class="form-control price" autocomplete="off"></td>';      

            htmlRows += '<td><input type="number" name="total[]" id="total_'+count+'" class="form-control total" autocomplete="off"></td>';          

            htmlRows += '</tr>';

            $('#invoiceItem').append(htmlRows);

        });

    });

});

</script>


查看完整回答
反对 回复 2022-10-28
  • 1 回答
  • 0 关注
  • 92 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号