locked
serialize checkbox value inside td when they are checked RRS feed

  • Question

  • User-1634604574 posted

    i have this table which it has a checkbox inside each td i want by clicking on button serialize that checkbox value when they are checked

    i wrote this code but is not working

    <table id="tb">

    <tr>
    <th>a1</th>
    <th>a2</th>
    </tr>

    <tr>
    <td><input type="checkbox" value="aa">aa</td>
    <td><input type="checkbox" checked value="bb">bb</td>
    </tr>

    <tr>
    <td><input type="checkbox" value="cc">cc</td>
    <td><input type="checkbox" checked value="dd">dd</td>
    </tr>

    <tr>
    <td><input type="checkbox" value="ee">ee</td>
    <td><input type="checkbox" value="ff">ff</td>
    </tr>


    </table>
    <br>
    <input type="button" value="ok" id="b">

    $("#b").on("click", function () {


    var AddressesDataJSON;

    $('input:checkbox:checked', '#tb').each(function () {
    var t=$(this).prop('value');
    AddressesDataJSON = $("#tb").find('input:checkbox').serializeArray();
    });


    alert(JSON.stringify(AddressesDataJSON));
    })

    Thursday, February 7, 2019 5:33 AM

Answers

  • User1520731567 posted

    Hi zhyanadil.it@gmail.com,

    You need to add name attribute on checkboxes,like the below:

     <table id="tb">
    
            <tr>
                <th>a1</th>
                <th>a2</th>
            </tr>
    
            <tr>
                <td><input type="checkbox" value="aa">aa</td>
                <td><input type="checkbox" checked name="bb" value="bb">bb</td>
            </tr>
    
            <tr>
                <td><input type="checkbox" value="cc">cc</td>
                <td><input type="checkbox" checked name="dd"  value="dd">dd</td>
            </tr>
    
            <tr>
                <td><input type="checkbox" value="ee">ee</td>
                <td><input type="checkbox" value="ff">ff</td>
            </tr>
    
    
        </table>

    Because:

    The .serializeArray() method uses the standard W3C rules for successful controls to determine which elements it should include; in particular the element cannot be disabled and must contain a name attribute. No submit button value is serialized since the form was not submitted using a button. Data from file select elements is not serialized. Elements that do not contain a value attribute are represented with the empty string value.

    How it work:

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 7, 2019 8:37 AM