2013-10-29 21 views
0

我有一些腳本,我已經創建了一個表中的Javascript, 其中一個列它有一個輸入。現在,在提交時,我會 喜歡獲取輸入中輸入的數據,以便它可以保存。 這是我的代碼。這將是醜陋的,對不起!JavaScript創建的輸入,現在我需要得到那些值

這裏是我如何創建我的表。 schedArray在另一個函數中創建。

function schedTable() { 
    var tableCode = '<table id="longCallData" width="99.5%"> <tr> <th>' + schedArray[1][0] + '</th> <th>' + schedArray[1][1] + '</th> <th>' + schedArray[1][2] + '</th><th>' + schedArray[1][3] + '</th> <th>' + schedArray[1][4] + '</th> <th>' + schedArray[1][5] + '</th><th>' + schedArray[1][6] + '</th> <th>' + schedArray[1][7] + '</th> <th>' + schedArray[1][8] + '</th><th>' + schedArray[1][9] + '</th> <th>' + schedArray[1][10] + '</th></tr>'; 
    var oddeven = new Boolean(); 

    for (i = 2; i < schedArray.length; i++) { 
     oddeven = !oddeven; 
     tableCode += 'enter code here<tr class="row' + oddeven + '">'; 
     tableCode += '<th>' + schedArray[i][0] + '</th> <th>**<input type="text" name="sched">**</th> <th>' + schedArray[i][2] + '</th><th>' + schedArray[i][3] + '</th> <th>' + schedArray[i][4] + '</th> <th>' + schedArray[i][5] + '</th><th>' + schedArray[i][6] + '</th> <th>' + schedArray[i][7] + '</th> <th>' + schedArray[i][8] + '</th><th>' + schedArray[i][9] + '</th> <th>' + schedArray[i][10] + '</th></tr>'; 
    } 

    tableCode += '</table>'; 
    var tabSpan = document.getElementById('shiftTable'); //Get display location 
    tabSpan.innerHTML = tableCode; //Place data in display element 

    var count = schedArray.length - 2; 
    var counted = schedArray.length - 2; 
    var ctnSpan = document.getElementById('countOne'); //Get display location 
    ctnSpan.innerHTML = count; //Place data in display element 
    var cntSpan = document.getElementById('countTwo'); //Get display location 
    cntSpan.innerHTML = counted; //Place data in display element 
} 

用戶添加自己的信息後,他們點擊提交按鈕的onclick哪=「提交()」。 筆記是文本在html中,選擇是列的var。 我已經嘗試了一些東西在這裏和那裏,包括使一個新的數組 和操縱,但我總是得到一個空。有一次,我能夠 獲得原始數組的頂部,但沒有該數組中的其他數據。

function submit() { 
    var message = confirm("This Will Submit Your Bid And Close Out The Form, Are You Sure You Are Ready To Submit?", ""); 
    if (message != null && message != "") { 
     var extras = document.getElementById('notes').value; 
     var choice = " "; 

     var s = Fo.OpenTextFile('file.csv', 8, true, 0); 

     s.WriteLine(user.Name + ',' + user.ID + ',,' + choice + ',' + extras); 
     s.Close(); 
     alert("Thank You!"); 
    } 
} 

如果我使用一個document.getElementBy * (「附表」)。用或超時值值, *已經姓名,班級,標記和名稱中使用其他代碼,但沒有成功。下面

是整個代碼可能會更好有用:

<!DOCTYPE html> 
<html> 

<head> 


    <HTA:APPLICATION ID="Shift_Bid" 
    APPLICATIONNAME="ShiftBid" 
    BORDER="thin" 
    CAPTION="yes" 
    SHOWINTASKBAR="yes" 
    SINGLEINSTANCE="Yes" 
    SYSMENU="yes" 
    selection="no" 
    minimize="yes" 
    > 

<title>Shift Bid</title> 

    <style type="text/css"> 
    body {overflow:hidden;} 

    #titleBid {position:absolute; top:10px; left:160px;} 
    #nameSpace {position:absolute; top:50px; left:200px;} 
    #oracleSpace {position:absolute; top:50px; left:400px;} 
    #agentName {border:1px solid black;} 
    #agentOracle {border:1px solid black;} 
    #info {position:absolute; top:70px; left:120px; font-size:20px; font-weight:bolder;} 
    #contentSpace {position:absolute; top:100px; left:5px; width:1250px; height:480px; border:1px solid black; overflow:hidden;} 
    #countBid {position:absolute; top:580px; left:120px; font-size:20px; font-weight:bolder;} 
    #notAtion {position:absolute; top:610px; left:5px; width:1250px; height:120px; border:1px solid black; overflow:hidden;} 
    #comment {position:absolute; top:5px; left:10px; font-size:15pt;} 
    #notes {position:absolute; top:30px; left:5px; width:1000px; height:80px; border:1px solid black; overflow:hidden;} 
    #action {position:absolute; top:2px; left:1016px; width:228px; height:114px; padding-left:2px; border:1px solid black; font-size:12pt; font-weight:bolder;} 
    #submit {position:absolute; top:79px; left:23px; width:180px; height:30px; background-color:green; font-size:15pt; font-weight:bolder;} 
    table input {width:15px;} 
    table {border-collapse:collapse; font-size:10pt; margin-left:2px;} 
    table th {border-top:1px solid black; font-weight:bolder;} 
    .rowfalse{background-color: #E6E6E6;} 
    .rowtrue{background-color: #FFFFFF;} 
    table th:hover {font-size:12pt;} 

    </style> 


<script> 


window.resizeTo(1310,800); 
var Fo = new ActiveXObject("Scripting.FileSystemObject"); 
var network = new ActiveXObject('WScript.Network'); 
var objSysInfo = new ActiveXObject("ADSystemInfo") 
var objUser = GetObject("LDAP://" + objSysInfo.UserName) 
var user = {'Name': objUser.displayName, 'ID': objUser.sAMAccountName} 
var dir = "//directory/of/file"; 


getForm(); 



function getAgent(){ 
    var name = user.Name; 
    var oracle = user.ID; 

var nameSpan = document.getElementById('agentName'); 
nameSpan.innerHTML = ' ' + name + ' '; 

var oracleSpan = document.getElementById('agentOracle'); 
oracleSpan.innerHTML = ' ' + oracle + ' '; 

    nameSpan.style.color = "#006600"; 
    oracleSpan.style.color = "#006600"; 
} 





function getForm() { 
var dataPath = dir + '/Agent_Form.csv'; 
if(Fo.FileExists(dataPath)){ 
    var dataFile = Fo.OpenTextFile(dataPath,1); 
    schedArray = new Array(); 
    while(!dataFile.AtEndOfStream){ 
     var line = dataFile.Line - 1; 
     schedArray[line] = dataFile.readLine(); 
    } 
    dataFile.close(); 
    for(i=0;i<schedArray.length;i++){ 
     schedArray[i] = schedArray[i].split(","); 
    } 
} 
} 



function getStuff(){ 
getAgent(); 
getTitle(); 
schedTable(); 
} 



function startUp(){ 
getForm(); 
getStuff(); 
} 


function getTitle(){ 
var title = schedArray[0][1]; 

var titleData = title; 


var titleSpan = document.getElementById('titleBid'); 
titleSpan.innerHTML = '<h1>' + titleData + '</h1>'; 

} 

function schedTable(){ 
var tableCode = '<table id="longCallData" width="99.5%"> <tr> <th>' + schedArray[1][0] + '</th> <th>' + schedArray[1][1] + '</th> <th>' + schedArray[1][2] + '</th><th>' + schedArray[1][3] + '</th> <th>' + schedArray[1][4] + '</th> <th>' + schedArray[1][5] + '</th><th>' + schedArray[1][6] + '</th> <th>' + schedArray[1][7] + '</th> <th>' + schedArray[1][8] + '</th><th>' + schedArray[1][9] + '</th> <th>' + schedArray[1][10] + '</th></tr>'; 
var oddeven = new Boolean(); 
for(i=2;i<schedArray.length;i++){ 
oddeven = !oddeven; 
tableCode += '<tr class="row'+ oddeven +'">'; 
tableCode += '<th>' + schedArray[i][0] + '</th> <th><input type="text" name="sched"></th> <th>' + schedArray[i][2] + '</th><th>' + schedArray[i][3] + '</th> <th>' + schedArray[i][4] + '</th> <th>' + schedArray[i][5] + '</th><th>' + schedArray[i][6] + '</th> <th>' + schedArray[i][7] + '</th> <th>' + schedArray[i][8] + '</th><th>' + schedArray[i][9] + '</th> <th>' + schedArray[i][10] + '</th></tr>'; 

} 

tableCode += '</table>'; 
var tabSpan = document.getElementById('shiftTable'); 
tabSpan.innerHTML = tableCode; 

var count = schedArray.length - 2; 
var counted = schedArray.length - 2; 
var ctnSpan = document.getElementById('countOne'); 
ctnSpan.innerHTML = count; 
var cntSpan = document.getElementById('countTwo'); 
cntSpan.innerHTML = counted; 

} 





function submit(){ 
var message = confirm("This Will Submit Your Bid And Close Out The Form, Are You Sure You Are Ready To Submit?", ""); 
if(message!=null && message!=""){ 
     var extras = document.getElementById('notes').value; 

     var tabSpan = document.getElementById('shiftTable'); 
     tabSpan.getElementsByName("sched")[0].value; 
var s = Fo.OpenTextFile('file.csv', 8, true, 0); 

s.WriteLine(user.Name+','+user.ID+',,'+tabSpan+','+extras); 
s.Close(); 
alert("Thank You!"); 
} 
} 


</script> 

</head> 

<body onload="startUp()"> 

<div id="main" > 

<div id="agentSpace"> 

<span id="titleBid"></span> 
<div id="nameSpace"> 
Name : 
<span id="agentName"> </span> 
</div> 
<div id="oracleSpace"> 
Oracle : 
<span id="agentOracle"> </span> 
</div> 
<div id="info">Please make sure to enter your name and oracle in the fields below.</div> 
</div> 
<div id="contentSpace"> 
<span id="shiftTable"></span> 
</div> 
<div id="countBid">Please list choices from 1-<span id="countOne"></span>, all fields need to be numbered. Top choice is 1, bottom choice is <span id="countTwo"></span>.</div> 
<div id="notAtion"><div id="comment">Comments: (ie. Public transportation needs to include what the earliest start and end times)</div><input id="notes" value=""><div id="action">Please Review Your Selections and Make Note if you have Concerns. Then Submit Your Bid. Thank You!<button id="submit"onclick="submit()">Submit</button></div></div> 
</div> 

</body> 

</html> 
+0

當你用'name'選擇時,你會得到一個回報。因此,如果只有一個名稱爲「input」的輸入,則需要訪問集合的索引「[0]」。我不知道你的'onsubmit()'所在的位置,但是如果每行有一個,你可以改變它一下,然後遍歷該行的'input'。 –

+0

如果您將乾淨的縮進應用於您的代碼,並使您的一些句子更清晰一些,那麼您的問題會更容易理解。 –

+0

我已經用整個代碼更新了條目.... onsubmit()在按鈕上的html中。 –

回答

0

也許這可能來自於事實,你叫getElementByName,而不是getElementsByName

getElementsByName返回一個數組,所以你應該嘗試這樣的:

document.getElementsByName("sched")[0].value 

但你必須確保只有一個名爲章附表,或者知道它的索引對象。也許你應該嘗試使用DOM: var tabSpan = document.getElementById('shiftTable');

// Create table 
table = document.createElement("table"); 

// Create table header lines 
tableHeaderLine = document.createElement("tr"); 
for (j = 0; i <= 10; j++) 
{ 
    cell = document.createElement("th"); 
    cell.innerHTML = schedArray[1][j]; 
    tableHeaderLine.appendChild(cell); 
} 

// Create table lines 
for (i = 2; i < schedArray.length; i++) 
{ 
    line = document.createElement("tr"); 
    table.appendChild(line); 

    for (j = 0; i <= 10; j++) 
    { 
     cell = document.createElement("th"); 

     if (j == 1) 
     { 
      schedInput = document.createElement("input"); 
      schedInput.setAttribute("type", "text"); 
      schedInput.name = "sched"; 
      cell.appendChild(schedInput); 
     } 
     else 
     { 
      cell.innerHTML = schedArray[i][j]; 
     } 
    } 
} 

// Append table to tabSpan 
tabSpan.appendChild(table); 

使用DOM將允許您阻止HTML解析器來調用錯誤的innerHTML 方法時。 在此代碼中,每次都會分別使用innerHTML,並且您在純DOM中創建輸入,因此您確定錯誤不是來自解析器。

+0

我已經試過這個和其他人,與該代碼它給了我一個錯誤「對象doesn」支持財產或方法'getElementByName'「,我已經看到它給出了這個錯誤幾次,但不是別人,與任何getElement –

+0

你確定你試過getElementsByName?你必須把一個S給Element,把它放在複數中。 – kube

+0

我已經添加了我的整個代碼....也許這將有助於...我已經添加了您的建議,但我已經從完整的代碼中刪除了它。我甚至嘗試過在提交函數中調用另一個函數來完成獲取和數組構建......仍然沒有任何東西 –

相關問題