2

我有这段代码:

<tr class="editField">
  <td>abc</td>
  <td>123</td>
  </td>
</tr>
<tr class="editField">
  <td>dfg</td>
  <td>456</td>
  </td>
</tr>

然后我有这个javascript来打开一个带有jQuery的对话框:

$( ".editField" ).click(function() {
        alert(...); // should get me 123
});

假设我点击第一个tr,我会得到第二个的内容td(即123),我应该写什么?

4

4 回答 4

13

鉴于您当前的加价,我建议:

$( ".editField" ).click(function() {
        alert($(this).find('td:eq(1)').text());
});

但是,虽然这在给定的示例中有效,但将一些定义属性添加到您希望从中恢复“值”的元素会更容易(并且随后更可靠),例如类名“值” ',像这样给出 HTML:

<tr class="editField">
  <td>abc</td>
  <td class="value">123</td>
</tr>

和 jQuery:

$( ".editField" ).click(function() {
        alert($(this).find('td.value').text());
});

或者,当然,如果“值”始终是任何给定行(相关类的)的最后一个 td元素,那么您可以改用:

$( ".editField" ).click(function() {
        alert($(this).find('td:last').text());
});

Incidentally, please note that your HTML is malformed, you have a surplus </td> after the last cell in both rows of your sample code.

References:

于 2012-08-29T22:33:14.150 回答
1

try this:

$( ".editField" ).click(function() {

    var clickedValue = $(this).find('td:first').next().text();

alert(clickedValue );    

});

fiddle link : http://jsfiddle.net/9cRRj/8/

于 2012-08-30T13:24:23.980 回答
1
  $(function () {
        $(".Cartbtn").click(function () {
            var temp = {
                "Id": $(this).data("itemid"),
                "Name": $(this).data("itemname"),
                "Price": $(this).data("itemprice"),
                "Quantity": $(this).data("itemqty"),
                "ImageUrl": $(this).data("itemimgurl"),
            });
于 2018-08-13T22:21:26.070 回答
0
$(document).bind("click", function (e) {
    $(e.target).closest("li").toggleClass("highlight");
    alert($(event.target).text());
});

this will directly give you the selected element id. $(event.target).text(); will give you the selected element text within a second.

于 2017-04-04T08:56:54.120 回答