簡體   English   中英

從html表格單元格獲取內容值,並使用javascript將其顯示在文本框中

[英]Get content value from a html table cell and make it appear to the textbox using javascript

我在從可滾動的HTML表的單元格中檢索值時遇到問題。 我已經修改了一些JavaScript代碼,但它們似乎無法正常工作。 請幫助我,我對javascript很陌生。 這是我的以下代碼:

Java腳本

var rowIndex = 0;
var cellIndex = 0;
var nTable = "";
var nRows = 0;
var nCells = 0;

function showCoords(r,c)
{
    rowIndex = r;
    cellIndex = c;
    if(r!=0 && nTable.rows[r].cells[c].tagName != "th")
    {
var selectedValue = nTable.rows[r].cells[0].innerHTML;
        document.getElementById('celldata1').value=selectedValue;
var selectedValue = nTable.rows[r].cells[1].innerHTML;
        document.getElementById('celldata2').value=selectedValue;
var selectedValue = nTable.rows[r].cells[2].innerHTML;
        document.getElementById('celldata3').value=selectedValue;
    }
}

function getCoords(currCell)
{
    for (i=0; i<nRows; i++)
    {
        for (n=0; n<nCells; n++)
        {
            if (nTable.rows[i].cells[n] == currCell)
            {
                showCoords(i,n);
            }
        }
    }
}

function mapTable()
{
    nTable = document.getElementsByTagName("table")[1]

    nRows = nTable.rows.length;
    nCells = nTable.rows[0].cells.length;
    for (i=0; i<nRows; i++)
    {
        for (n=0; n<nCells; n++)
        {
            nTable.rows[i].cells[n].onclick = function()
            {
                getCoords(this);                    
            }
            nTable.rows[i].cells[n].onmouseover = function()
            {
                this.style.cursor = 'pointer';              
            }
        }
    }

}

onload=mapTable;

我不確定上面的javascript代碼在語法或邏輯上是否出錯,但是如果有人幫助我指出這些錯誤,我會感到高興。

HTML代碼:

    <head>
        <meta http-equiv="content-type" content="text/html;charset=utf-8" />
        <title>My Content</title>
        <link rel="stylesheet" text="css/text" href="mycss.css" title=""></link> 
        <script type ="text/javascript" src="test.js"></script>     
    </head>

身體部位1

這是我希望值從tableone的每個單元格顯示在每個文本框中的部分。

    <body>

           <table border="0">
                <tbody>

                    <tr>
                        <td>Content 1:</td>
                        <td><input id="celldata1" type="text" name="content1" value="" /></td>
                    </tr>

                    <tr>
                        <td>Content 2:</td>
                        <td><input id="celldata2" type="text" name="content2" value="" /></td>
                    </tr>

                    <tr>
                        <td>Content 3:</td>
                        <td><input id="celldata3" type="text" name="content3" value="" /></td>
                    </tr>

                </tbody>
            </table>

身體部位2

這是桌布。

            <table border="1" id="tableone">
                <thead>
                    <tr>
                        <th>Item</th>
                        <th>Content 1</th>
                        <th>Content 2</th>
                        <th>Content 3</th>

                    </tr>
                </thead>

                <tbody> 
                    <%! int i;%>

                    <%for(int i=0; i<50; i++){%>

                        <tr>
                        <td><%=i+1%></td>
                        <td></td>
                        <td></td>
                        <td></td
                        </tr>

                    <%}%>
            </tbody>
            </table>
        </div>

順便謝謝你,如果我的英語不好我很抱歉:)

我為此使用jQuery:

// Iterate throught rows of table:
jQuery("table").find("tr").each( function() {

    var currentRow = jQuery(this);

    // Iterate throught cell in current row: 
    currentRow.find("td").each(function() {

        var currentCell = jQuery(this);
        // Do what you want with cell
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM