繁体   English   中英

如何在JQuery中通过复选框获取选定列的值?

[英]How to get a value of selected column by checkbox in JQuery?

目前,我从该行获取所有输出。 是否只能获取所选“点”的值。 我怎样才能做到这一点?

选中后的预期输出:SF01 SF02 SF03

检查时的电流输出:SF01 11.5 3.5328 120.3359

<table border="1" width="100%" id="selectTable" name="selectTable">
    <legend>Display Data Points</legend>
    <tr>
        <th></th>
        <th width="40%">Point</th>
        <th width="20%">Depth</th>
        <th width="20%">Lat</th>
        <th width="20%">Long</th>
    </tr>
    <tr>
        <td>
            <input type="checkbox" class="checkBox" name="checkBox[]" />
        </td>
        <td>SF01</td>
        <td>11.5</td>
        <td>3.5328</td>
        <td>120.3359</td>
    </tr>
    <tr>
        <td>
            <input type="checkbox" class="checkBox" name="checkBox[]" />
        </td>
        <td>SF02</td>
        <td>41.5</td>
        <td>6.5328</td>
        <td>113.3359</td>
    </tr>
    <tr>
        <td>
            <input type="checkbox" class="checkBox" name="checkBox[]" />
        </td>
        <td>SF03</td>
        <td>82.1</td>
        <td>5.2828</td>
        <td>721.9059</td>
    </tr>
</table>

<script type="text/javascript">
    function clickedBox() {

        var values = new Array();

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

        });

        document.getElementById("boxArea").value = "=====Results=====\n" + values.join("\n");
    }

    $(document).ready(function() {
        $("#selectPoint").click(clickedBox);
    });
</script>

siblings('td')更改为next('td') ,我希望您需要将event绑定到checkbox

$.each($("input[name='checkBox[]']:checked").closest("td").next("td"), function () {
                                                         //^^^^here
    values.push($(this).text());
});

DEMO

当您说siblings ,它将获取first td所有siblings ,这就是为什么要获取所有值的原因。

为了更简单-

使用data属性将值放在element上。

<input type="checkbox" data-value="SF02"/>

jQuery-

$(function(){
  values= new Array();
  $.each($('input[type="checkbox"]:checked'),function(index,item){
    values.push(item.data('value');
  });
});

暂无
暂无

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

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