jQuery Jquery获取选定复选框的表行值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18565669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:57:38  来源:igfitidea点击:

Jquery to get table row values of selected checkboxes

jquery

提问by divz

In the following example http://jsfiddle.net/hU89p/200/

在下面的例子中 http://jsfiddle.net/hU89p/200/

How can I get the row values of selected check boxes on the onClick function

如何获取 onClick 函数上选定复选框的行值

          Select   Cell phone        Rating     Location
                   BlackBerry9650    2/5        UK
                   Samsung Galaxy    3.5/5      US
                   Droid X           4.5/5      REB

in the following table while selecting multiple checkboxes I have to alert the corresponding row values .

在下表中选择多个复选框时,我必须提醒相应的行值。

function myfunc(ele) {

 var values = new Array();
       $.each($("input[name='case[]']:checked"), function() {
       values.push($(this).val());
       alert("val---"+values);

           )};
              }

For example:

例如:

while selecting first two checkboxes I have to get BlackBerry9650, 2/5, UK, and Samsung Galaxy,3.5/5 ,US

在选择前两个复选框时,我必须 get BlackBerry9650, 2/5, UK, and Samsung Galaxy,3.5/5 ,US

How it is possible?

怎么可能?

回答by Itay

jsFiddle Demo

jsFiddle 演示

If you want them to be separated with commas and spaces you can push every cell on its own and use JS Array .join()

如果您希望它们用逗号和空格分隔,您可以单独推送每个单元格并使用 JS Array .join()

var values = new Array();

$.each($("input[name='case[]']:checked").closest("td").siblings("td"),
       function () {
            values.push($(this).text());
       });

   alert("val---" + values.join(", "));

回答by trrrrrrm

$("input[name='case[]']").click(function(){
 var values = new Array();
       $.each($("input[name='case[]']:checked"), function() {
           var data = $(this).parents('tr:eq(0)');
           values.push({ 'callphone':$(data).find('td:eq(1)').text(), 'rating':$(data).find('td:eq(2)').text() , 'location':$(data).find('td:eq(3)').text()});             
       });

       console.log(values);
 });

Example: http://jsfiddle.net/hU89p/213/

示例:http: //jsfiddle.net/hU89p/213/

for your custom output check this:

对于您的自定义输出,请检查:

Example: http://jsfiddle.net/hU89p/215/

示例:http: //jsfiddle.net/hU89p/215/

回答by Unknown

Try:

尝试:

function myfunc(ele) {
 var values = new Array();
       $.each($("input[name='case[]']:checked").parents("td").siblings(), function() {
           values.push($(this).text());
           alert("val---"+values);
       });
}

DEMO FIDDLE

DEMO FIDDLE

回答by Jacques

I've updated your Fiddle a bit, the resulting code looks like this:
HTML:

我已经更新了您的 Fiddle,结果代码如下所示:
HTML:

<table border="1">
    <tr>
        <th>Select</th>
        <th>Cell phone</th>
        <th>Rating</th>
        <th>Location</th>
    </tr>
    <tr>
        <td align="center">
            <input type="checkbox" class="case" name="case" value="1" />
        </td>
        <td>BlackBerry Bold 9650</td>
        <td>2/5</td>
        <td>UK</td>
    </tr>
    <tr>
        <td align="center">
            <input type="checkbox" class="case" name="case" value="2" />
        </td>
        <td>Samsung Galaxy</td>
        <td>3.5/5</td>
        <td>US</td>
    </tr>
    <tr>
        <td align="center">
            <input type="checkbox" class="case" name="case" value="3" />
        </td>
        <td>Droid X</td>
        <td>4.5/5</td>
        <td>REB</td>
    </tr>
    </tr>
</table>

Script:

脚本:

$('[name=case]').click(function () {
    checkAll();
});

function checkAll() {
    $('[name=case]:checked').each(function () {
        alert('selected: ' + $(this).val());
    });
}