gpt4 book ai didi

javascript - 表单提交给出未定义的值

转载 作者:行者123 更新时间:2023-12-03 05:43:05 26 4
gpt4 key购买 nike

我有一个正在生成的表格,它有一个排序按钮。我想获取用户想要排序的列,但当我尝试从“byColumn”获取值时,我不断得到未定义的排序值:一些帮助将不胜感激:

function createTable(data){
var str = "<form id='tableSelect' action='javascript:void(0);'><table><thead><tr> <th>TicketNum</th><th>Recieved</th><th>SenderName</th><th>Sender Email</th><th>Subject</th><th>Tech</th><th>Status</th><th>Select</th></tr></thead><tbody>";

for(var key in data){
if (!data.hasOwnProperty(key)) continue;
var row = data[key];
str += "<tr> <td>";
str += row['TicketNum'] + "</td><td>";
str += row['Recieved'] + "</td><td>";
str += row['SenderName'] + "</td><td>";
str += row['SenderEmail'] + "</td><td>";
str += row['Subject'] + "</td><td>";
str += row['Tech'] + "</td><td>";
str += row['Status'] + "</td><td>";
str += "<input type='radio' name ='selectRow' value=" +row['TicketNum'] + ">" + "</td></tr>";
}
str += "<tr><td> Sort By: <input type = 'radio' name = 'byColumn' value='TicketNum'><td> Sort By: <input type = 'radio' name = 'byColumn' value='Recieved'><td> Sort By: <input type = 'radio' name = 'byColumn' value='SenderName'><td> Sort By: <input type = 'radio' name = 'byColumn' value='SenderEmail'><td> Sort By: <input type = 'radio' name = 'byColumn' value='Subject'><td> Sort By: <input type = 'radio' name = 'byColumn' value='Tech'><td> Sort By: <input type = 'radio' name = 'byColumn' value='Status'><td> <button type ='button' value='Submit' button class=\"myButton\" onclick=\"sort();\">Sort</button> </tr>";

str += "</tbody></table></form>";
console.log(str);
document.getElementById("table").innerHTML = str;
}
function sort(){
var table = currentTable;
var sortby = document. getElementsByName("byColumn").value; //the error is on this line
alert(sortby);

}

enter image description here

最佳答案

您需要使用 .getElementsByName("byColumn") 来获取 name='byColumn' 而非 .getElementById("byColumn")< 的所有输入。然后循环所有 radio 以获取 .checked .value

var data = [{
'TicketNum':'TicketNum',
'Recieved':'Recieved',
'SenderName':'SenderName',
'SenderEmail':'SenderEmail',
'Subject':'Subject',
'Tech':'Tech',
'Status':'Status'
},{
'TicketNum':'TicketNum',
'Recieved':'Recieved',
'SenderName':'SenderName',
'SenderEmail':'SenderEmail',
'Subject':'Subject',
'Tech':'Tech',
'Status':'Status'
},{
'TicketNum':'TicketNum',
'Recieved':'Recieved',
'SenderName':'SenderName',
'SenderEmail':'SenderEmail',
'Subject':'Subject',
'Tech':'Tech',
'Status':'Status'
}];
createTable(data);
function createTable(data) {
var str = "<form id='tableSelect' action='javascript:void(0);'><table><thead><tr> <th>TicketNum</th><th>Recieved</th><th>SenderName</th><th>Sender Email</th><th>Subject</th><th>Tech</th><th>Status</th><th>Select</th></tr></thead><tbody>";

for (var key in data) {
if (!data.hasOwnProperty(key)) continue;
var row = data[key];
str += "<tr> <td>";
str += row['TicketNum'] + "</td><td>";
str += row['Recieved'] + "</td><td>";
str += row['SenderName'] + "</td><td>";
str += row['SenderEmail'] + "</td><td>";
str += row['Subject'] + "</td><td>";
str += row['Tech'] + "</td><td>";
str += row['Status'] + "</td><td>";
str += "<input type='radio' name ='selectRow' value=" + row['TicketNum'] + ">" + "</td></tr>";
}
str += "<tr><td> Sort By: <input type = 'radio' name = 'byColumn' value='TicketNum'><td> Sort By: <input type = 'radio' name = 'byColumn' value='Recieved'><td> Sort By: <input type = 'radio' name = 'byColumn' value='SenderName'><td> Sort By: <input type = 'radio' name = 'byColumn' value='SenderEmail'><td> Sort By: <input type = 'radio' name = 'byColumn' value='Subject'><td> Sort By: <input type = 'radio' name = 'byColumn' value='Tech'><td> Sort By: <input type = 'radio' name = 'byColumn' value='Status'><td> <button type ='button' value='Submit' button class=\"myButton\" onclick=\"sort();\">Sort</button> </tr>";

str += "</tbody></table></form>";
//console.log(str);
document.getElementById("table").innerHTML = str;
}

function sort() {
//var table = currentTable;
var col = document.getElementsByName("byColumn"); //the error is on this line
var sortby;
for (var i = 0; i < col.length; i++) {
if (col[i].checked) {
sortby = col[i].value;
break;
}
}
alert("sort by: "+sortby);

}
<div id='table'></div>

关于javascript - 表单提交给出未定义的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40444515/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com