我有一个正在生成的表,它有一个排序按钮。我想获取用户想要排序的列,但是当我尝试从“ 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);

    }


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

最佳答案

您需要使用.getElementsByName("byColumn")来获取所有带有name='byColumn'而不是.getElementById("byColumn")的输入。然后遍历所有收音机以获得.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>

10-08 12:06