[英]how to save second column value into text file from table when first column checkbox is checked?
html頁面在第一列中的其他復選框中包含表復選框作為值。 我想在選中復選框並單擊保存按鈕時將第二列數據保存到文本文件中。我提供了文本框來輸入文件名。 用戶可以選中全部或任何復選框,並在文本框中輸入文本文件名,以將數據保存到文件中。 問題是,單擊“保存”按鈕時,它僅采用第二列的第一值。 即使我選中所有復選框。
它殺了我,請有人幫忙...
搗鼓這一點: - 小提琴
<!DOCTYPE html>
<html>
<head>
<script>
function checkAll(ele) {
var checkboxes = document.getElementsByTagName('input');
if (ele.checked) {
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].type == 'checkbox') {
checkboxes[i].checked = true;
}
}
} else {
for (var i = 0; i < checkboxes.length; i++) {
console.log(i)
if (checkboxes[i].type == 'checkbox') {
checkboxes[i].checked = false;
}
}
}
}
</script>
<script>
function Info() {
next_page='ccc.php'
checkboxes = document.getElementsByTagName("input");
for (var i = 0; i < checkboxes.length; i++) {
var checkbox = checkboxes[i];
checkbox.onclick = function() {
var currentRow = this.parentNode.parentNode;
var secondColumn = currentRow.getElementsByTagName("td")[1];
form = document.createElement('form');
form.setAttribute('method', 'POST');
form.setAttribute('action', next_page);
opt = 'save';
myvar = document.createElement('input');
myvar.setAttribute('name', opt);
myvar.setAttribute('type', 'text');
myvar.setAttribute('value', opt);
form.appendChild(myvar);
text = secondColumn.textContent;
myvar1 = document.createElement('input');
myvar1.setAttribute('name', 'text');
myvar1.setAttribute('type', 'text');
myvar1.setAttribute('value', text);
form.appendChild(myvar1);
document.body.appendChild(form);
form.submit();
};
}
}
</script>
</head>
<body>
<table id="testlist" border=1>
<tr style="font-family:verdana;font-size:16px"><th>Select All<input type="checkbox" style="zoom:1.5" onchange="checkAll(this)" name="chk[]"></th><th>TestCase Name</th> <th>TestCase Group</th> <th>Description</th></tr>
<tr><td><input type="checkbox" style="zoom:1.5"><td>A</td> <td>ALL_A</td> <td>This Test A</td></tr>
<tr><td><input type="checkbox" style="zoom:1.5"><td>B</td> <td>ALL_B</td> <td>This Test B</td></tr>
<tr><td><input type="checkbox" style="zoom:1.5"><td>C</td> <td>ALL_C</td> <td>This Test C.</td></tr>
<tr><td><input type="checkbox" style="zoom:1.5"><td>D</td> <td>ALL_D</td> <td>This Test D.</td></tr>
</table>
<label for="tc_file"><b>Enter File Name: </b></label><input type="text" name="tc_file" placeholder="Like ABC">
<input type="button" name="save" onClick="Info()" value="Save to File" style="height:35px;width:110px"/> <input type="button" onclick="location.href='add_newtc.php';" value="Add New Test" style="height:35px;width:110px"/>
</div>
<?php
$fileName = "tc_list.txt";
$tc_name = $_POST["text"];
ob_start();
echo "$tc_name";
$obStr = ob_get_contents();
ob_end_clean();
file_put_contents($fileName, $obStr." \n", FILE_APPEND);
?>
</body>
</html>
這是我為您解決的問題:
<table id="testlist" border=1>
<tr style="font-family:verdana;font-size:16px">
<th>Select All
<input type="checkbox" style="zoom:1.5" onchange="checkAll(this)" name="chk[]">
</th>
<th>TestCase Name</th>
<th>TestCase Group</th>
<th>Description</th>
</tr>
<tr>
<td>
<input type="checkbox" style="zoom:1.5">
<td>A</td>
<td>ALL_A</td>
<td>This Test A</td>
</tr>
<tr>
<td>
<input type="checkbox" style="zoom:1.5">
<td>B</td>
<td>ALL_B</td>
<td>This Test B</td>
</tr>
<tr>
<td>
<input type="checkbox" style="zoom:1.5">
<td>C</td>
<td>ALL_C</td>
<td>This Test C.</td>
</tr>
<tr>
<td>
<input type="checkbox" style="zoom:1.5">
<td>D</td>
<td>ALL_D</td>
<td>This Test D.</td>
</tr>
</table>
<label for="tc_file"><b>Enter File Name: </b></label>
<input type="text" name="tc_file" placeholder="Like ABC">
<input type="button" name="save" onClick="Info()" value="Save to File" style="height:35px;width:110px" />
<input type="button" onclick="location.href='add_newtc.php';" value="Add New Test" style="height:35px;width:110px" />
<script language=javascript>
function checkAll(ele) {
var checkboxes = document.getElementsByTagName('input');
if (ele.checked) {
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].type == 'checkbox') {
checkboxes[i].checked = true;
}
}
} else {
for (var i = 0; i < checkboxes.length; i++) {
console.log(i)
if (checkboxes[i].type == 'checkbox') {
checkboxes[i].checked = false;
}
}
}
}
function Info() {
var next_page = 'ccc.php'
var itemCount = 0;
var table = document.getElementById("testlist");
var rows = table.rows;
var form = document.createElement('form');
form.setAttribute('method', 'POST');
form.setAttribute('action', next_page);
for (var i = 1; i < rows.length; i++) {
row = rows[i];
cells = row.cells;
firstCol = cells[0];
secondCol = cells[1];
if (firstCol.firstElementChild.checked) {
myvar1 = document.createElement('input');
myvar1.setAttribute('name', 'tc' + (itemCount++));
myvar1.setAttribute('type', 'text');
myvar1.setAttribute('value', secondCol.innerHTML);
form.appendChild(myvar1);
}
}
if (itemCount > 0) {
document.body.appendChild(form);
form.submit();
}
}
這是因為您沒有為每個復選框指定唯一的名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.