简体   繁体   中英

JQuery datatable link onclick event

I have a jquery datatable that contains multiple columns and rows as below.

<table>
    <thead>
        <tr>
            <td>Name</td>
            <td>Class</td>
            <td>Action</td>
            <td>Score</td>
            <td>Corrected Score</td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Student1</td>
            <td>Math</td>
            <td><a href="#">Add 5</a>
            </td>
            <td>73</td>
            <td>
                <input type="text" id="1_final_score" />
            </td>
        </tr>
        <tr>
            <td>Student2</td>
            <td>Biology</td>
            <td><a href="#">Add 5</a>
            </td>
            <td>84</td>
            <td>
                <input type="text" id="2_final_score" />
            </td>
        </tr>
        <tr>
            <td>Student3</td>
            <td>History</td>
            <td><a href="#">Add 5</a>
            </td>
            <td>50</td>
            <td>
                <input type="text" id="3_final_score" />
            </td>
        </tr>
    </tbody>

When i click on the "Add 5" link, I want to add 5 to the score in the corresponding row and insert the result in the final score input field. I have tried using the jquery row-select feature, but am unable to figure out how to accomplish the above.

$(document).ready(function () {
$('table').find('a').click(function () {
    var original = parseInt($(this).parents('tr').find('td:nth-child(4)').text()) + 5;

    $(this).parents('tr').find('input[type=text]').val(original);

   });

});

You can try this:

$(document).ready(function(){
    $("a").on("click", function(){
        var num=parseInt($(this).closest("td").next("td").text());
        num=num+5;
        $(this).closest("td").next("td").text(num);
        $(this).closest("tr").find("input").val(num);

    });
});

FIDDLE

Try with this

$('a:contains("Add 5")').click(function(){
    $(this).parents('tr').find('td input:text').val(+$(this).parent().next('td').text() + 5 )
 });

FIddle : http://jsfiddle.net/2n0bevxo/172/

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