如何获得与当前位置最接近的元素的值?

时间:2014-09-03 14:23:17

标签: javascript jquery

<table>
    <tr style="background-color: aqua">
        <td>Malzeme No</td>
        <td>Malzeme Adı</td>
    </tr>
    <asp:Repeater ID="rptMalzemeList" runat="server">
        <ItemTemplate>
            <tr class="malzlist">
                <td><%#Eval("MATNR") %></td>
                <td><%#Eval("MAKTX") %></td> // I want this value
                <td><a href="#" id="btnSec" onclick="sendValue(this)">Seç</a></td>
            </tr>
        </ItemTemplate>
    </asp:Repeater>
</table>

我的JavaScript功能如下,

function sendValue(objs)
{
    var MalzName = objs.parent().parent().children().next().text();
    alert(MalzName);
    window.opener.HandlePopupResult(MalzName);
    window.close();
}

但是,它给出了错误undefined是一个无功能。你能帮帮我吗?

3 个答案:

答案 0 :(得分:2)

变化:

var MalzName = objs.parent().parent().children().next().text();

为:

var MalzName = $(objs).parent().prev().text();

答案 1 :(得分:0)

objs是一个DOM元素,而不是一个jQuery包装器。你的函数的第一行应该转换它:

objs = $(objs);

然后你有机会开始工作。目前没有评论该功能的其他方面。

答案 2 :(得分:0)

您可能希望避免使用内联JS并使用以下内容:

HTML:

<td><a href="#" id="btnSec">Seç</a></td>

JS

$(function() {
   $('#btnSec').on('click',function() {
       var MalzName = $(this).parent().prev().text();
       alert(MalzName);
       window.opener.HandlePopupResult(MalzName);
       window.close();
    });
});

OR:

$(function() {
    $('#btnSec').on('click', sendValue);
});

function sendValue()
{
    var MalzName = $(this).parent().prev().text();
    alert(MalzName);
    window.opener.HandlePopupResult(MalzName);
    window.close();
}