简体   繁体   English

使用 JavaScript/jQuery 获取 html 表中一行的选定下拉值

[英]Get selected drop-down value of a row in html table using JavaScript/jQuery

I have a html table with 4 columns and multiple rows我有一个 4 列多行的 html 表

column1 : simple text
column2 : simple text
column3 : select list with 2 values
column4 : Button that performs some operation

At run time, I want to get the selected / entered values for all the columns of that row against which column4 button is clicked.在运行时,我想为单击column4按钮的那一行的所有列获取选定/输入的值。 I know a little of JavaScript/jQuery.我知道一点 JavaScript/jQuery。

Here is the static table code这是静态表代码

<table id="invoiceTbl" border="1">

    <thead>
        <tr>
            <th>Broker info</th>
            <th>Receivable Amount</th>
            <<th>Status</th>
                <th>Action</th>
        </tr>
    </thead>
    <tbody></tbody>
</table>

And I am populating table ie inserting data on AJAX call like this:我正在填充表,即在 AJAX 调用上插入数据,如下所示:

$.ajax({
    type: "POST",
    data: $('#searchDetails').serialize(),
    async: false,
    cache: false,
    url: "/opsadmin/search.json",
    dataType: 'json',
    success: function (result) {
        var i = 1;
        $.each(result, function (index, value) {
            alert(index + ": " + value.brokerInfo);
            $('table#invoiceTbl TBODY').append('<tr> <td>' + i + '</td><td>' + value.brokerInfo + '</td><td>' + value.receivableAmount + '</td><td><select name="act" id="s"><option>PENDING</option><option>CLEARED</option></select></td><td><input type="button" value="Process" onclick="updateStatus(\'' + index + '\')"</input></td></tr>');
            i++;
        });
    }
});
return false;

On #button click you will get an array (arr) that holds the values of the clicked row cells:#button单击上,您将获得一个数组 (arr),其中包含单击的行单元格的值:

 $('#button').on('click', function() { var arr = []; $(this).closest('tr').find('td').each(function() { var that = $(this); if (that.find('input[type="text"]').length > 0) { arr.push(that.find('input[type="text"]').val()); } else if (that.find('select').length > 0) { arr.push(that.find('select').val()); } else { arr.push(that.text()); } }); alert(arr); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table> <tbody> <tr> <td>text</td> <td>text2</td> <td> <select> <option value="opt1">opt1</option> <option value="opt2">opt2</option> <option value="opt3">opt3</option> </select> </td> <td><button id="button">Click</button></td> </tr> </tbody> </table>

$("#invoiceTbl input[type=button]").click(function(e){
   $(e.target).closest("tr").find("td").slice(0,2).each(function(){
        console.log($(this).text());
   });

});

First set the unique ID to each of your "select" with help of index like:首先在索引的帮助下为每个“选择”设置唯一 ID,例如:

'<select name="act" id="s'+ index +'">'

and pass the index with "updateStatus(index)" function call.并通过“updateStatus(index)”函数调用传递索引。

function updateStatus(index) {
    $('#s'+index).val(); // value of selected box.
}

There's already a few good answers, but my solution is a bit different:已经有一些很好的答案,但我的解决方案有点不同:

$("#invoiceTbl tr").each(function(){
    var select = $(this).find("select");
    var button = $(this).find("button");
    button.click(function(){
        alert(select.val());
    });
});

If you also want the content of the other columns, that's a bit more difficult:如果您还想要其他列的内容,那就有点困难了:

$("#invoiceTbl tr").each(function(){
    var tr = $(this);
    var button = $(this).find("button");
    button.click(function(){
        tr.find("td").each(function(){
            var select = $(this).find("select");
            var td_button = $(this).find("button");
            if(!td_button.length)
            {
                if(select.length)
                    console.log(select.val());
                else
                    console.log($(this).text());
            }
        });
    });
});

Check out https://jsfiddle.net/cgfjewoe/ to see it run.查看https://jsfiddle.net/cgfjewoe/以查看它的运行情况。

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

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