document.getElementById无法使用动态ID生成

时间:2015-06-20 09:00:35

标签: javascript dom

<!DOCTYPE html><!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
--><html><head><title>Inventory Master - Material Inwards</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width">
    <link rel="stylesheet" href="Inwards.css"/>
    <link rel="stylesheet" href="bootstrap-3.3.2-dist/css/bootstrap.css" type="text/css">
    <link rel="stylesheet" href="bootstrap-3.3.2-dist/css/bootstrap.min.css" type="text/css"/>
    <script src="jquery-2.1.3.min.js"></script>
    <script src="Inwards.js"></script>
</head>
<body>
    <div id="operation">
        <div class="container-fluid">
            <div id="actbtngrp">
                <button class="actbtn">EDIT</button>
                <button class="actbtn">PRINT</button>
                <button class="actbtn">DELETE</button>
            </div>
        </div>
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-3" id="dh">Date</div>
                <div class="col-md-3" id="dh">Vendor Name</div>
            </div>
            <div class="row">
                <div class="col-md-3"><input type="text" id="date" readonly></div>
                <div class="col-md-3"><input type="text" id="vendor" onkeydown="searchvendor(event)"></div>
            </div>
        </div>
        &nbsp;
        <div class="container-fluid">
            <table id="input">
                <thead>
                    <tr>
                        <th>Q</th>
                        <th>Level 1</th>
                        <th>Level 2</th>
                        <th>Product Description</th>
                        <th>$$</th>
                        <th>Qty.</th>
                        <th>Rate</th>
                        <th>PER</th>
                        <th>UOM</th>
                        <th>$</th>
                        <th>Disc</th>
                        <th>GoRate</th>
                        <th>Amount</th>
                    </tr>
                </thead>
            </table>
            &nbsp;
            <div>
            <button class="actbtn" id="add" onclick="addRow()">ADD RECORD</button>
            <button class="actbtn" id="save">SAVE N MOVE</button>
            </div>
        </div>
    </div>
</body>

{
var q = 1;var o = "-";var id = 0;var idData = $(":input").serializeArray();
var currentid;var nextid;var previousid;var master = "tubemaster";
$(document).ready(function() {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1;
var yyyy = today.getFullYear();
if (dd < 10) {
    dd = '0' + dd;
}
if (mm < 10) {
    mm = '0' + mm;
}
var today = dd + '/' + mm + '/' + yyyy;
document.getElementById("date").value = today;
var table = document.getElementById("input");
var row = document.createElement("tr");
row.setAttribute("id", id);
idData[id] = id;
table.appendChild(row);
id++;
for (i = 0; i < 1; i++) {
    var td = document.createElement("td");
    td.innerHTML = q;
    row.appendChild(td);
    td.setAttribute("id", id);
    idData[id] = id;
    q++;
    id++;
}
for (i = 0; i < 1; i++) {
    var td = document.createElement("td");
    td.setAttribute("contenteditable", "true");
    row.appendChild(td);
    td.setAttribute("id", id);
    idData[id] = id;
    id++;
    td.onkeydown = function(e) {
        currentid = parseInt(this.id);
        if (e.keyCode === 13) {
            e.preventDefault();
            nextid = currentid + 1;
            document.getElementById(nextid).focus();
        }
        if (e.ctrlKey) {
            if (e.keyCode === 46) {
                tempid = currentid - 2;
                $('#' + tempid).remove();
            }
        }
        if (e.keyCode === 40) {
            e.preventDefault();
            nextid = currentid + 14;
            document.getElementById(nextid).focus();
        }
        //write escape key function
    };
}
for (i = 0; i < 1; i++) {
    var td = document.createElement("td");
    td.setAttribute("contenteditable", "true");
    row.appendChild(td);
    td.setAttribute("id", id);
    idData[id] = id;
    id++;
    td.onkeydown = function(e) {
        currentid = parseInt(this.id);
        if (e.keyCode === 13) {
            e.preventDefault();
            nextid = currentid + 3;
            document.getElementById(nextid).focus();
        }
        if (e.keyCode === 27) {
            e.preventDefault();
            previousid = currentid - 1;
            document.getElementById(previousid).focus();
        }
        if (e.keyCode === 40) {
            e.preventDefault();
            nextid = currentid + 14;
            document.getElementById(nextid).focus();
        }
        if ((e.keyCode !== 40) || (e.keyCode !== 27) || (e.keyCode !== 13)){
            if (e.keyCode === 13) {
                e.preventDefault();
                nextid = currentid + 3;
                document.getElementById(nextid).focus();
            } else {

                var a = parseInt(this.id);
                var s = a.toString();
                alert(a);
                alert(typeof a);
                alert(typeof s);
                var read = "Hello";
                alert(read);
  

问题在这里......我在获取文件后没有读取var值.getElementById(s).value

                read = document.getElementById(s).value;
                alert(read);
            }
        }
    };
}
for (i = 0; i < 1; i++) {
    var td = document.createElement("td");
    row.appendChild(td);
    td.setAttribute("id", id);
    idData[id] = id;
    id++;
}
for (i = 0; i < 1; i++) {
    var td = document.createElement("td");
    row.appendChild(td);
    td.setAttribute("id", id);
    idData[id] = id;
    id++;
}
for (i = 0; i < 1; i++) {
    var td = document.createElement("td");
    td.setAttribute("contenteditable", "true");
    row.appendChild(td);
    td.setAttribute("id", id);
    idData[id] = id;
    id++;
    td.onkeydown = function(e) {
        currentid = parseInt(this.id);
        if (e.keyCode === 13) {
            e.preventDefault();
            nextid = currentid + 4;
            document.getElementById(nextid).focus();
        }
        if (e.keyCode === 27) {
            e.preventDefault();
            previousid = currentid - 3;
            document.getElementById(previousid).focus();
        }
        if (e.keyCode === 40) {
            e.preventDefault();
            nextid = currentid + 14;
            document.getElementById(nextid).focus();
        }
    };
}
for (i = 0; i < 1; i++) {
    var td = document.createElement("td");
    row.appendChild(td);
    td.setAttribute("id", id);
    idData[id] = id;
    id++;
}
for (i = 0; i < 1; i++) {
    var td = document.createElement("td");
    row.appendChild(td);
    td.setAttribute("id", id);
    idData[id] = id;
    id++;
}
for (i = 0; i < 1; i++) {
    var td = document.createElement("td");
    row.appendChild(td);
    td.setAttribute("id", id);
    idData[id] = id;
    id++;
}
for (i = 0; i < 1; i++) {
    var td = document.createElement("td");
    td.innerHTML = o;
    td.setAttribute("contenteditable", "true");
    row.appendChild(td);
    td.setAttribute("id", id);
    idData[id] = id;
    id++;
    td.onkeydown = function(e) {
        currentid = parseInt(this.id);
        if (e.keyCode === 13) {
            e.preventDefault();
            nextid = currentid + 1;
            document.getElementById(nextid).focus();
        }
        if (e.keyCode === 27) {
            e.preventDefault();
            previousid = currentid - 4;
            document.getElementById(previousid).focus();
        }
        if (e.keyCode === 40) {
            e.preventDefault();
            nextid = currentid + 14;
            document.getElementById(nextid).focus();
        }
    };
}
for (i = 0; i < 1; i++) {
    var td = document.createElement("td");
    td.innerHTML = "";
    td.setAttribute("contenteditable", "true");
    row.appendChild(td);
    td.setAttribute("id", id);
    idData[id] = id;
    id++;
    td.onkeydown = function(e) {
        currentid = parseInt(this.id);
        if (e.keyCode === 13) {
            e.preventDefault();
            nextid = currentid + 1;
            document.getElementById(nextid).focus();
        }
        if (e.keyCode === 27) {
            e.preventDefault();
            previousid = currentid - 1;
            document.getElementById(previousid).focus();
        }
        if (e.keyCode === 40) {
            e.preventDefault();
            nextid = currentid + 14;
            document.getElementById(nextid).focus();
        }
    };
}
for (i = 0; i < 1; i++) {
    var td = document.createElement("td");
    td.innerHTML = "";
    td.setAttribute("contenteditable", "true");
    row.appendChild(td);
    td.setAttribute("id", id);
    idData[id] = id;
    id++;
    td.onkeydown = function(e) {
        currentid = parseInt(this.id);
        if (e.keyCode === 13) {
            e.preventDefault();
            document.getElementById("add").focus();
        }
        if (e.keyCode === 27) {
            e.preventDefault();
            previousid = currentid - 1;
            document.getElementById(previousid).focus();
        }
        if (e.keyCode === 40) {
            e.preventDefault();
            nextid = currentid + 14;
            document.getElementById(nextid).focus();
        }
    };
}
for (i = 0; i < 1; i++) {
    var td = document.createElement("td");
    td.innerHTML = "";
    row.appendChild(td);
    td.setAttribute("id", id);
    idData[id] = id;
    id++;
}
document.getElementById("vendor").focus();});

基本上我正在为输入用户输入生成运行时ID我希望阅读它但我无法用&#34; this.id&#34;也不是通过&#34; 3&#34;。 3是动态运行时生成的id。

Plz帮助.....

2 个答案:

答案 0 :(得分:0)

/*
    document.getElementById("date").value = today;   -- doesn't exist as far as I can see. maybe remove the quotes or use the var today?

全局变量读取

    read = document.getElementById(s).value;  // A global var
    alert(read);

哪个var读?在循环中重复定义的全局var读取或本地var读取(“Hello”)?

    if ((e.keyCode !== 40) || (e.keyCode !== 27) || (e.keyCode !== 13)){
    ....var read = "Hello";
    }
*/

document.getElementById("Hello")可能未定义。

答案 1 :(得分:0)

我们需要使用.innerHtml而不是.value

'read = document.getElementById(s).innerHtml;'