简体   繁体   English

使用Javascript或JQuery从特定的表行和列中获取选择值和选项

[英]Get Select Value and Option from Specific Table Row and Column with Javascript or JQuery

I have a table that has rows appended to it. 我有一张表,上面附加了行。 Each row has a select in column 2 {0, 1, [2]} . 每行在第2列{0, 1, [2]}都有一个select。 I am trying to get the text/value/option of the select using JavaScript or JQuery. 我正在尝试使用JavaScript或JQuery获取选择的文本/值/选项。 This seems like it should be something easy but for the life of me I cannot figure out a way to get the select element in the selected row & column. 看来这应该很简单,但是对我来说,我无法找出一种方法来获取所选行和列中的select元素。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Here is my table. 这是我的桌子。

 <div id="table_container">
        <table id="computer_table" class="service_table">
            <thead id="header_container">
            <th class="table_header">COMPUTER NAME</th>
            <th class="table_header">LIVE/HIDE</th>
            <th class="table_header">MODE</th>
            <th class="table_header">RUN</th>
            </thead>
            <tbody id="table_body">
            </tbody>
        </table>
    </div>

Here is my JavaScript that adds rows to the table. 这是我的JavaScript,它向表中添加行。 Rows contain a select in column 2. 行在第2列中包含一个选择。

row = $('<tr class="row_white"><td class="tb_pc_name">' + value.pc_name + '</td><td class="tb_live_hide">LIVE</td><td class="tb_mode">' +
                        '<select id="select_mode" class="select_dropdown">' +
                        '<option value="0">0 - DEFAULT</option>' +
                        '<option value="1">1 - CLEAN UP</option>' +
                        '<option value="2">2 - SIGN IN</option>' +
                        '<option value="3">3 - SIGN OUT</option>' +
                        '<option value="4">4 - UPDATE IPSW</option>' +
                        '<option value="5">5 - OPEN DMG DIRECTORY</option>' +
                        '</select></td>' +
                        '<td class="tb_btn"><button type="button" id="btn_run">RUN</button></td></tr>');


                    $("#computer_table").append(row);

After all rows have been appended, the user can make a selection from the select. 在附加所有行之后,用户可以从选择中进行选择。 There is a button in column 3 that when clicked should show an alert with the select option and value of the select on the same row. 第3列中有一个按钮,单击该按钮应在同一行上显示带有select选项和select值的警报。 The code below should show the selected option and/or value of the select in the row of the cell that is clicked. 下面的代码应在单击的单元格行中显示所选的选项和/或select的值。 I can get the row and column but cannot get the value of the select. 我可以获取行和列,但无法获取选择的值。

 $("#computer_table td").click(function() {

                    var table = document.getElementById('computer_table');

                    var column_num = parseInt( $(this).index() );
                    var row_num = parseInt( $(this).parent().index() );

                    alert('test');

               /*Error here.*******************************************************************************************/
                    var combo = table.rows[row_num + 1].cells[2].getElementById('select_mode');
 //$("#select_mode option:contains("?").attr('selected', 'selected');
                    alert(combo.value);

                });

First of all there is something wrong with youre code that add the row. 首先,您添加行的代码有问题。 Every ID must be unique and it's not the case. 每个ID都必须是唯一的,事实并非如此。 Try using a counter (or something else) to avoid that like this: 尝试使用计数器(或其他方法)来避免这种情况:

var numberRowAdded = 0;

row = $('<tr class="row_white"><td class="tb_pc_name">' + value.pc_name + '</td><td class="tb_live_hide">LIVE</td><td class="tb_mode">' +
                        '<select id="select_mode'+ numberRowAdded  +'" class="select_dropdown">' +
                        '<option value="0">0 - DEFAULT</option>' +
                        '<option value="1">1 - CLEAN UP</option>' +
                        '<option value="2">2 - SIGN IN</option>' +
                        '<option value="3">3 - SIGN OUT</option>' +
                        '<option value="4">4 - UPDATE IPSW</option>' +
                        '<option value="5">5 - OPEN DMG DIRECTORY</option>' +
                        '</select></td>' +
                        '<td class="tb_btn"><button type="button" id="btn_run'+ numberRowAdded  +'">RUN</button></td></tr>');


                    $("#computer_table").append(row);
numberRowAdded++;

Then to select what you want : 然后选择您想要的:

$("#computer_table td").click(function() {
    alert($(this).find(".select_dropdown").val());

});

Hope this help. 希望能有所帮助。

try passing in the ID onClick: 尝试传递ID onClick:

$("#computer_table td").click(function(this.id){    }

Just make sure all your cells have an ID. 只要确保您所有的单元格都有一个ID。

Here is W3 example on enclosures and encapsulation. 这是有关机箱和封装的W3示例。 This is useful for giving all your elements a different ID. 这对于为所有元素赋予不同的ID很有用。 W3 encapsulation example W3封装示例

Adding elements with eventlisteners attached, and passing in a unique id!! 添加带有事件监听器的元素,并传递唯一的ID! *Note: this is not an exact fix for your code. *注意:这不是您的代码的确切解决方案。 This should however aid you in understanding. 但是,这应该有助于您理解。

/*use this function to create unique id's*/
 var add = (function () {
    var counter = 0;
    return function () {return counter += 1;}
})();


/*grab your table*/
 var table =document.getElementById("computer_table"); 
 /* create an element, set it's attributes*/
 var newRow = document.createElement("th");
 newRow.setAttribute("class", "table_header");
 newRow.setAttribute("id", "table_header"+add());
/*add the event listener*/ 
 newRow.addEventListener("click",function(this.id){  /* YOUR CODE HERE */ });  
 /*append to your table*/
 table.appendChild(newRow); 

If you changed your buttons to use a class rather than an id they could be triggered with this: 如果您将按钮更改为使用类而不是ID,则可以通过以下方式触发它们:

$("#computer_table").on("click", "button.btn_run", function(e) {
    alert($(this).parent().prev().children().find(":selected").text());
}); 

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

相关问题 获取选项中的“值”属性 <select>使用Javascript或Jquery - Get the “value” attribute of an option in a <select> with Javascript or Jquery 通过TD类和jQuery中单元格中的特定值从表中获取行 - Get row from a table by td class and specific value in the cell in jquery 从特定选择选项获取属性值 - Get Attribute Value From Specific Select Option 在来自 javascript/jquery 的选择选项值中设置双引号字符串,也从 javascript/jquery 获取 - Set double quotes string in select option value from javascript/jquery and also get from javascript/jquery 如何在jquery中从表行和列获取所有值到数组 - How to get all value from table row and column to array in jquery 使用 JavaScript 从特定表中获取特定行? - Get a specific row from specific table with JavaScript? Javascript从html表的特定行中获取列的文本值 - Javascript get the text value of a column from a particular row of an html table 如何从javascript中的表中获取列名和第一行值? - How to get column name and first row value from table in javascript? 单击行后,JQuery / JavaScript从表中获取值 - JQuery / JavaScript to get value from table after clicking on row 如何从数据库表中获取行值以在JavaScript / jQuery中使用它? - How to get a row value from database table to use it in JavaScript / jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM