亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在多行上使用 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 數據庫中的選擇選項?!癶tmlRows”中給出的字段應該在用戶想要使用 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 關注
  • 123 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號