开发者

jquery removeClass in current row

I have edit button at the last cell of the table. when I click it I am validating inputs if something goes wrong I 开发者_运维百科am adding error class to necessary inputs. when the user will correct mistakes and push edit button again I want to remove error class of current row. but not from all row. How to do it?


In the click handler:

$(this).closest('tr').removeClass('errorClass');

Or if it was from the 'input' elements:

$(this).closest('tr').find(':input').removeClass('errorClass');


Your edit button click handler might look like this:

$("selector_for_table tr input[name=edit]").click(function() {
    var row = $(this).closest('tr');
    if (everythingIsValid()) {
        row.removeClass('invalid');
    }
    else {
        row.addClass('invalid');
   }
});

That uses closest to find the first parent element of the clicked button that's a tr.

...although you might look at delegate for this if you're using an up-to-date jQuery:

$("selector_for_table").delegate('input[name=edit]', 'click', function() {
    var row = $(this).closest('tr');
    if (everythingIsValid()) {
        row.removeClass('invalid');
    }
    else {
        row.addClass('invalid');
   }
});

...if the rows are dynamic (e.g., if you add or remove them). delegate just sets a handler on the parent element, and then watches for the event to bubble. If it sees the event, and the source element of the event matches the given selector, it fires the handler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜