简体   繁体   中英

Add options to each select box in html table row dynamically

I have a dynamically created HTML table. In each row is a dropdown, I need to add options to the dropdown. My code, however, will only add values to the first row.

Here is how I am creating the table and trying to add the options.

        $.post(     

       'dataform.php',
        { functionname: JSON.stringify('operation'), args:part},
        function(response) {
        result = JSON.parse(response);



            block = []
            for (var item in result){

              var objectItem = result[item];

          var opnum = objectItem.opnum;
          var opdesc = objectItem.opdesc;
          var wc = objectItem.wc;
          var sel = document.createElement("select");
              sel.type = "select";
              sel.id = "sel";
              sel.value = wc;
          var activity = objectItem.activity;
          var machine = objectItem.machine;
          var direct_labor = objectItem.direct_labor;

            block.push(opnum);
            block.push(opdesc);
            block.push(wc);
            block.push(sel);
            block.push(activity);
            block.push(machine);
            block.push(direct_labor);
            ops.push(block);
            block = [];

                }

            for (var i = 0; i < ops.length; i++) {
            tr = document.createElement('tr');
            tr.appendChild(document.createElement('td'));
            tr.appendChild(document.createElement('td'));
            tr.appendChild(document.createElement('td'));
            tr.appendChild(document.createElement('td'));
            tr.appendChild(document.createElement('td'));
            tr.appendChild(document.createElement('td')); 



            tr.cells[0].appendChild(document.createTextNode(ops[i][0]));
            tr.cells[1].appendChild(document.createTextNode(ops[i][1]));
            tr.cells[2].appendChild(ops[i][3]);
            tr.cells[3].appendChild(document.createTextNode(ops[i][4]));
            tr.cells[4].appendChild(document.createTextNode(ops[i][5])); 
            tr.cells[5].appendChild(document.createTextNode(ops[i][6]));
            table.appendChild(tr);

            }

            tablearea.appendChild(table);           


        }, 
    )

$.post(
    'dataform.php',
    {functionname: JSON.stringify('wc')},
    function(response) {
        result = JSON.parse(response);

            block = []
            for (var item in result){

            var objectItem = result[item];

            var wrkc = objectItem.wc
            var act_key = objectItem.activity;

            debugger;
            $.each($('#sel').append($("<option></option>").html(wrkc)));

        }


    }

)   


}) 

I have tried having the the options appended each time the cell is created and I had the same results.

Any help would be greatly appreciated.

Thanks!

('#sel') the ID selector uses document.getElementById() internally, which can return only an element. So it will add to just one. Use a class if you want to append it to each element that contains the said class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM