[英]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.