Выберите тег ввода в той же строке из таблицы, щелкнув тег привязки в той же строке

Образец HTML (это содержит только один из многих других строк).

<table id="ctl00_ContentPlaceHolder1_categoryTable" class="categoryTable sortAsc editInputs" border="0">
    <tbody>
     <tr>
     <td>8</td>
     <td>
     <input name="ctl00$ContentPlaceHolder1$ctl17" type="text" value="307349_335692806541952_16061425_n.jpg" readonly="readonly" />
    </td>
    <td><input name="ctl00$ContentPlaceHolder1$ctl18" type="text" value="key1 " readonly="readonly" /></td>
    <td>3/28/2013</td>
    <td>.jpg</td>
    <td>28120</td>
    <td><a href="download.aspx filename=307349_335692806541952_16061425_n.jpg&type=image">307349_335692806541952_16061425_n.jpg</a></td>
    <td>
    <a href="javascript:void(0)" class="editLinkClass">Edit </a><a href="javascript:void(0)" class="deletetLinkClass"> Delete</a><a href="javascript:void(0)" class="updateLinkClass" style="display:none;">Update</a><a href="javascript:void(0)" class="cancelLinkClass" style="display:none;"> Cancel</a>
    </td>
    <tr>
    </tbody>

Мой джаваскрипт

$(document).ready(function () {
            console.log("document ready")
            $(".categoryTable tr td a").click (function (event) {
                console.log($(this).text() + "click detected");
                if ($(this).text() === "Edit ") {//compare with "Edit " not "Edit"
                    console.log("Edit");
                    console.log($(this).parent().parent().children('td input').text());
                    $(this).siblings(".deletetLinkClass").attr("style", "display:none");
                    $(this).siblings(".updateLinkClass").attr("style", "display:;");
                    $(this).siblings(".cancelLinkClass").attr("style", "display:;");
                    $(this).attr("style", "display:none")
                }
                else
                    console.log("no EDit");
            });
        });

Что я пытаюсь сделать, это выбрать теги ввода в той же строке tr но разные td по щелчку тега привязки в той же строке. Я пробовал много комбинаций следующего утверждения jQuery безуспешно. Пожалуйста помоги.$(this).parent().parent().children('td input').text() Вот this представляет тег привязанного клика. Чтобы быть более понятным, я не хочу выбирать все входные теги в таблице, только те, которые находятся в одной строке.

3 ответа

Решение

С помощью parents() а также find(),

and to get the value of input, use val() и не text()..

$(this).parent().parent().children('td input').text();
                                           //--^^^^^^---here  

попробуй это

var inputs=$(this).parents('tr').find('input');
$.each(inputs,function(){  //<---- loop since you have multiple input
    console.log($(this).val()); //.val() since val() is used to get the input value..
})

или используя closest()а также find(),

var inputs=$(this).closest('tr').find('input');

using context

var inputs = $('td input', $(this).closest('tr')).val();

здесь работает скрипка

Вы можете сделать это следующим образом:

$('td input', $(this).closest('tr')).val();

Попробуйте код ниже, чтобы найти входные данные из данной строки

$(this).closest('tr').find('td input').text()
Другие вопросы по тегам