简体   繁体   English

Javascript获取表行内不同单元格类型的值

[英]Javascript get values of different cell types inside a table row

Good Evening, i write a script to get the value of any row that's clicked, but my problem is that any row contains different types of controls, it may be a text field <input type="text" id="speed" /> and it may be a dropdown 晚上好,我编写了一个脚本来获取被单击的任何行的值,但是我的问题是任何行都包含不同类型的控件,它可能是文本字段<input type="text" id="speed" />这可能是一个下拉列表

    <select name="engineType">
       <option value="1">Manual</option>
       <option value="2">Automatic</option>
       <option value="3">Both</option>
    </select>

in the first case i will get the innerHtml, in the second case i will get the value (value of selected item) so how to check the type of each cell before deciding to get the value or innerHtml, here's the script: 在第一种情况下,我将获取innerHtml,在第二种情况下,我将获取值(所选项目的值),因此在决定获取值或innerHtml之前如何检查每个单元格的类型,这是脚本:

function findRowNumber() {
    var rowIdx;
    var rowData = new Array();
    var table = document.getElementById('product_table');
    var rows = table.getElementsByTagName('tr');
    var selectedRow;
    for (var i = 0; i < rows.length; i++) {
        rows[i].onclick = function() {
            rowIdx = this.rowIndex;
            selectedRow = rows[rowIdx];
            for (var j = 1; j <11; j++) {
                var rowCell = selectedRow.cells[j].innerHTML;
                rowData.push(rowCellValue);
                alert(rowCellValue);

            }

        }

    }
}

I think this might be what you're looking for, though as the system said, perhaps .innerHTML is not the best way of doing things. 我认为这可能是您正在寻找的内容,尽管正如系统所说, .innerHTML并不是最好的处理方式。

for (var j = 1; j <11; j++) {
    var rowCell = selectedRow.cells[j];
    var inputElements;
    if((inputElements = rowCell.getElementByTagName("select")).length > 0)
    {
        // Do stuff for select
    }
    else if((inputElements = rowCell.getElementByTagName("input")).length > 0)
    {
        // Do stuff for input
    }

    rowData.push(rowCellValue);
    alert(rowCellValue);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM