JavaScript创建了输入,现在我需要获取这些输入的值

JavaScript created Inputs, now i need to get the value of those

本文关键字:输入 获取 创建 JavaScript      更新时间:2023-09-26

我有一些脚本,我在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="submit()"。注释是文本在 HTML 中,选择是列的 var。我在这里和那里尝试了一些事情,包括制作一个新数组并操纵它,但我总是得到一个 Null。有一次我能够获取原始数组的顶部,但不获取该数组中的其他数据。

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*('sched').value,* 已使用其他代码的名称、类、标签和名称,但没有成功。

下面是整个代码可能更有用:

<!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>

也许它可能来自你调用getElementByName而不是getElementsByName的事实。

getElementsByName 返回一个数组,所以你应该像这样尝试:

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

但是您必须确保只有一个名为 sched 的对象,或者知道它的索引。

也许你也应该尝试使用 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 将允许您在调用 innerHTML 方法时防止来自 HTML 解析器的错误。在此代码中,innerHTML 每次都单独使用,并且您在纯 DOM 中创建输入,因此您可以确定错误不是来自解析器。