简体   繁体   English

从表格单元格内的下拉列表中获取所选值

[英]Getting selected value from, dropdown list placed inside table cell

I have a HTML table as follows. 我有一个HTML表,如下所示。 I want retrieve row values from this table. 我想从该表中检索行值。 For retrieving table row values I have written this java script but i'm getting value null for this variable, var score = comrow.cells[2].childNodes[0].selectedIndex; 为了检索表行值,我已经编写了此Java脚本,但是我为此变量获取了null值,var score = comrow.cells [2] .childNodes [0] .selectedIndex;

how can I correctly retrieve selected values of the drop down list? 如何正确检索下拉列表的选定值?

function adddata(tableID){

  var phraseTable = document.getElementById(tableID);
        var phrrowCount = phraseTable.rows.length;


        for(var i=0; i<phrrowCount; i++) {
            var comrow = phraseTable.rows[i];
           // var comchkbox = comrow.cells[0].childNodes[0];
            var comval= comrow.cells[1].childNodes[0].value;
            var score = comrow.cells[2].childNodes[0].selectedIndex;
           } 
}

<TABLE id="dataTable" name="topkphrase" cellpadding="0" cellspacing="0" border="1">


            <tbody>
                    <TD width="5%"><INPUT type="checkbox" name="chk"/> </TD>
                    <TD width="75%"><input type="text" name="txt" value="" size="100%"/></TD>
                    <TD width="20%">
                        <SELECT name="score">

                            <OPTION value="vimp">V.Important</OPTION>
                            <OPTION value="imp">Important</OPTION>
                            <OPTION value="avr">Average</OPTION>

                        </SELECT>
                    </TD>
            </tbody>
        </TABLE>

Instead of trying to find the <select> object from the table, you can give the <select> a id and use getElementById. 可以尝试给<select>一个ID并使用getElementById,而不是尝试从表中查找<select>对象。

var selectElement = document.getElementById(selectID);
console.log(selectElement.value);
console.log(selectElement.selectedIndex);

You could also use getElementByName to find all select statements with the name "score". 您也可以使用getElementByName查找名称为“ score”的所有select语句。

var selectElement = document.getElementByName("score")[0];
console.log(selectElement.value);
console.log(selectElement.selectedIndex);

You may also want to look into JQuery for doing this as it may make things much easier for you. 您可能还需要研究JQuery来执行此操作,因为这可能会使您更轻松。

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

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