简体   繁体   中英

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

Currently I get all the output from the row. Is it possible to get the value of selected "Points" only. How can I do this?

Expected output when checked: SF01 SF02 SF03

Current output when checked: 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>

Change siblings('td') to next('td') and I hope you need to bind event to checkbox .

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

DEMO

When you say siblings then it will fetch all the siblings of first td , which is why you are getting all the values.

To keep it more simple-

Put your value using data attributes on 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');
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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