简体   繁体   English

如何选中和取消选中复选框,然后在表的最后一列中创建一列

[英]How to check and uncheck a checkbox and then create a column in the last column of the table

Night all. 晚上都。 I have a question here. 我在这里有一个问题。 I have a table that created by javascript function I made. 我有一个由我创建的javascript函数创建的表。 In that table contain a checkbox in each row and the thead has the checkbox too. 在该表的每一行中都包含一个checkboxthead也具有该checkbox I have separate the id's of the checkbox for the each row use id mycheckbox and for the thead use id mycheckbox1 . 我为每行使用id mycheckboxthead使用id mycheckbox1分别设置了checkbox的ID。

OK now to the problem. 现在就可以解决问题了。 I have to try to make all of my checkbox can be checked with the checkbox in the thead with id mycheckbox1 . 我必须尝试使所有我的复选框都可以通过ID为mycheckbox1thead的复选框进行检查。 I have try many code that I have found. 我尝试了许多发现的代码。 But no one working. 但是没有人在工作。 And I try make a fiddle with a table that I manually created with HTML and I can check all the texboxt and it work fine like this fiddle https://jsfiddle.net/minervaz/29fnt79f/1/ 然后我尝试用我用HTML手动创建的表做一个小提琴,我可以检查所有texboxt,它可以像这样的小提琴一样正常工作https://jsfiddle.net/minervaz/29fnt79f/1/

But when I try to use that in my page it can't work this is the code for the table that I have created 但是,当我尝试在页面中使用它时,它无法工作,这是我创建的表的代码

 $(document).ready(function(){ $('#submit-file').on("click",function(e){ if ($('#files').val()== "") { alert("Anda Harus Memasukkan File Terlebih Dahulu"); } else{ e.preventDefault(); $('#files').parse({ config: { delimiter: "", skipEmptyLines: false, complete: displayHTMLTable, }, before: function(file, inputElem) { //console.log("Parsing file...", file); }, error: function(err, file) { //console.log("ERROR:", err, file); }, complete: function() { //console.log("Done with all files"); } }); } }); function displayHTMLTable(results) { var table = "<table class='table table-bordered' width='90%'>"; var data = results.data; var size = -1; var check = 7; var header = "<thead><tr>"; header+= "<th width='120'>Kode Material</th>"; header+= "<th width='140'>Storage Location</th>"; header+= "<th width='130'>Movement Type</th>"; header+= "<th width='130'>Id Indentifier</th>"; header+= "<th width='120'>Date Input</th>"; header+= "<th width='80'>Netto</th>"; header+= "<th width='50'>Unit</th>"; header+= "<th width='80'>Payroll</th>"; header+= "<th><input type='checkbox' id='mycheckbox1' name='mycheckbox1' ></th>"; header+= "</tr></thead>"; table += header; table+="<tbody>"; for (i = 1; i < data.length; i++) { table += "<tr>"; var row = data[i]; var cells = row.join(",").split(","); if (cells.length < size) continue; else if (cells.length > size) size = cells.length; if (cells.length > check){ alert('Data Yang Anda Masukkan Salah'); } else{ for (j = 0; j < cells.length; j++) { var a = 1.000; var b = 10.000; var c = 20.000; var d = 45.000; var callback1 = '10.000'; var callback2 = '20.000'; var callback3 = '37.500'; var callback4 = '45.000'; table += "<td>"; table += cells[j]; table += "</td>"; } if (cells[5]> a && cells[5] <b){ table += "<td>"+ callback1 +"</td>" } else if (cells[5]> b && cells[5] <c){ table += "<td>"+ callback2 +"</td>" } else if (cells[5]> c && cells[5] <d){ table += "<td>"+ callback3 +"</td>" } table += "<td><input type='checkbox' id='mycheckbox' name='mycheckbox'></td>" table += "</tr>"; } } table+="</tbody>"; table += "</table>"; $("#parsed_csv_list").html(table); } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.3.5/papaparse.min.js"></script> <div class="container" style="padding:5px 5px; margin-left:5px"> <div class="well" style="width:70%"> <div class="row"> <form class="form-inline"> <div class="form-group"> <label for="files">Upload File Data :</label> <input type="file" id="files" class="form-control" accept=".csv" required /> </div> <div class="form-group"> <button type="submit" id="submit-file" class="btn btn-primary">Upload File</button> </div> </form> </div> </div> <div class="row" style="width:90%"> <form action="" id="form_data" name="form_data" method="post"> <div id="parsed_csv_list" class="panel-body table-responsive" style="width:90%"> </div> </form> </div> 

After I can checked my checkbox I want to make a new th column in the last row of the thead and in the last row of the each tbody . 在可以选中我的复选框之后,我想在thead的最后一行和每个tbody的最后一行中添加一个新的th列。 The value of the column is Tilting when it checked and Borongan when unchecked. 选中时该列的值是Tilting ,不选中该列的值是Borongan

for the csv file you can download in this link https://drive.google.com/open?id=0B_zAPPvH-idZZkxSRDI4NGNHOHc 对于csv文件,您可以在此链接中下载https://drive.google.com/open?id=0B_zAPPvH-idZZkxSRDI4NGNHOHc

The problem you are having is the element you are setting change event to is not present at the time of binding, so you will have to set it on a element which is present. 您遇到的问题是绑定时将更改事件设置为不存在的元素,因此您必须将其设置在存在的元素上。 I could give you the answer but that won't help in long run.As I am not a very good at explaining concepts I will just point you to the problem you are having with answer in it - 我可以给你答案,但是从长远来看不会有帮助。由于我不太擅长解释概念,因此我只将指出问题的答案-

jQuery .on('change', function() {} not triggering for dynamically created inputs jQuery .on('change',function(){}不触发动态创建的输入

For second part research more. 对于第二部分的研究更多。 (Also I did not understand) (我也不明白)

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

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