简体   繁体   English

如何动态填充表格中的单元格?

[英]How do I fill a cell in the table dynamically?

How do I fill a cell in the table dynamically after the user selects an option within the column?用户在列中选择一个选项后,如何动态填充表格中的单元格?

Inside the select there is an update function and it is working, but I can't update column two of the line with the data given value of the select.在 select 内部有一个更新 function 并且它正在工作,但我无法使用 select 的数据给定值更新该行的第二列。

 function update(line) { // update in the col of mytable for next col. // whats wrong with this code? // Why not working? var td = line.closest('td'); td.nextSibling.innerText = line.value; }
 <table id="myTable"> <thead> <tr> <td>Selection</td> <td>Value</td> </tr> </thead> <tbody> <tr> <td> <div class='form-group col-md-12 col-xs-12'> <select id="item1" onChange='update(this)' class='options' name='choice'> <option value='option1'>option1</option> <option value='option2'>option2</option> <option value='option3'>option3</option> <option value='option4'>option4</option> </select> </div> </td> <td id='emilio'> 1 </td> </tr> <tr> <td> <div class='form-group col-md-12 col-xs-12'> <select id="item1" onChange='update(this)' class='options' name='choice'> <option value='option1'>option1</option> <option value='option2'>option2</option> <option value='option3'>option3</option> <option value='option4'>option4</option> </select> </div> </td> <td> 2 </td> </tr> </tbody> </table>

Since the onChange is working, you need to select the containing td then update then next one like:由于onChange正在工作,您需要 select 包含td然后更新下一个,例如:

function onChange(update) {
            var td = update.closest('td');
            td.nextElementSibling.innerText = update.value;
        }

Your function name should be update, try this您的 function 名称应该更新,试试这个

 function update(e) { //update in the col of mytable for 0 if( e.id ==='item1'){ document.getElementById('selection1').innerHTML= e.value }else{ document.getElementById('selection2').innerHTML= e.value } }
 <table id="myTable"> <thead> <tr> <td>Selection</td> <td>Value</td> </tr> </thead> <tbody> <tr> <td> <div class='form-group col-md-12 col-xs-12'> <select id="item1" onChange='update(this)' class='options' name='choice'> <option value='1'>option1</option> <option value='2'>option2</option> <option value='3'>option3</option> <option value='4'>option4</option> </select> </div> </td> <td id='selection1'> 1 </td> </tr> <tr> <td> <div class='form-group col-md-12 col-xs-12'> <select id="item2" onChange='update(this)' class='options' name='choice'> <option value='option1'>option1</option> <option value='option2'>option2</option> <option value='option3'>option3</option> <option value='option4'>option4</option> </select> </div> </td> <td id='selection2' ></td> </tr> </tbody> </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何动态用背景色填充表格单元格? - How can I dynamically fill a table-cell with background-color? 如何动态地将html表格单元格数据插入到React中带有动态标题的表格中? - How do I dynamically insert html table cell data into a table with dynamic headers in React? 如何将RadToolTip添加到JQuery动态生成的表格单元格文本中 - How do I add a RadToolTip to a JQuery dynamically generated table cell text 如果表格是动态生成的,如何在点击时更改html单元格的颜色? - How do I change an html cell's color on click, if the table is generated dynamically? 如何在javascript中将输入字段动态添加到我的html表格行内的单元格中? - How do I dynamically add an input field into a cell inside my html table row in javascript? 如何使用JQuery动态填充转置的HTML表? - How can I dynamically fill a transposed HTML table with JQuery? 如何打开新窗口/选项卡并动态填充内容? - How do I open a new window/tab and dynamically fill it with content? 如何动态创建大小相同的 div 来填充屏幕? - How do I dynamically create equally sized divs to fill the screen? 我如何获取表格的单元格ID - How do i get the cell id of a table 你如何使Ace编辑器成长并以div相同的方式填充表格单元格? - How do you make Ace Editor Grow and fill a table cell in the same fashion a div does?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM