在单击同一行中的锚标记时,从表中的同一行中选择输入标记

时间:2013-05-15 06:00:18

标签: javascript jquery html-table htmltable-control

示例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>

我的Javascript

$(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 个答案:

答案 0 :(得分:2)

使用parents()find()

要获取输入值,请使用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');

使用上下文

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

working fiddle here

答案 1 :(得分:2)

你可以这样做:

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

答案 2 :(得分:0)

尝试使用以下代码查找给定行的输入

$(this).closest('tr').find('td input').text()