2016-11-05 31 views
0

我有一個我正在生成的表,它有一個排序按鈕。我想獲得用戶想要進行排序的列,但我不斷收到在不大不小的未定義的值,當我嘗試從「通過柱」獲得的價值: 一些幫助,將不勝感激:表單提交給定未定義的值

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

+0

如果你的問題是關於'NULL'值,那麼這是一個SQL問題。如果您重新提出問題並添加查詢,可能會很有用。 – Eugene

+0

我的意思是我提交表單時得到的空值...所以在排序函數的第二行 –

+0

更新:使用'getElementsByName(「byColumn」)'並循環遍歷所有檢查巫婆檢查以獲取值 看到這個https://jsfiddle.net/MamdouhFreelancer/yrub1mac/1/ –

回答

1

您需要使用.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>

0

您需要將id =「byColumn」添加到名爲「byColumn」的輸入中。

該ID由瀏覽器使用,該名稱由服務器使用。

由於標籤上沒有id,document.getElementById返回null,null不具有value的屬性。

+0

我實際上是想使用getElementsByName(「byColumn)。但即使這個改變,我沒有得到一個未定義的值 –