简体   繁体   English

jQuery-获取表单元格值

[英]jQuery - Get table cell value

I have a table which looks like the following. 我有一张看起来像下面的桌子。 The price normally comes from a database this is just for showing the problem I have. 价格通常来自数据库,仅用于显示我的问题。

 $(document).ready(function() { $(document).delegate('.amount input[type="text"]','keyup',(function() { var $this = $(this); var sub_total = $this.find('.sub_total'); var price = $this.find('.price').text(); var amount = $this.val(); alert(price); })); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td><input type="text" name="tickets" id="tickets" class="amount" maxlength="2" autocomplete="off"></td> <td>Ticket:</td> <td class="price">20,50</td> <td class="sub_total"></td> </tr> </table> 

What I would like to do is: when the user types a number in the field tickets it should update the field sub_total . 我想做的是:当用户在字段tickets输入数字时,应该更新sub_total字段。 With the jQuery I have this is not working. 使用jQuery,我无法正常工作。 When I alert(price) I get undefined . 当我alert(price)undefined So I wonder how to make the sub_total field with auto updated values. 所以我想知道如何使sub_total字段具有自动更新的值。 It might also be interesting to know that I have more rows like this beneath this row. 知道我在此行下还有更多行可能也很有趣。 So the script should only update the field from one specific row at a time. 因此,脚本一次只能更新一个特定行中的字段。

What I already tried but without success: How to get a table cell value using jQuery ; 我已经尝试过但没有成功的事情如何使用jQuery获取表单元格值 How to get a table cell value using jQuery? 如何使用jQuery获取表格单元格值? ; ; How to get table cell values any where in the table using jquery 如何使用jQuery在表中的任何位置获取表单元格值

Thanks in advance. 提前致谢。

Change you code like this 像这样更改您的代码

$(document).ready(function() {
    $(document).on('input[type="text"].amount', 'keyup', (function() {
        var $this = $(this);
        var sub_total = $this.closest("tr").find('.sub_total');
        var price = $this.closest("tr").find('.price').text();
        var amount = $this.val();
        alert(price);
    }));
});

find() will search for the children. find()将搜索子级。 So you should get into the parent tr first before using find() . 因此,在使用find()之前,您应该先进入父级tr

You can use closest("tr") to get the parent tr element 您可以使用closest("tr")来获取父级tr元素

You need to do traverse back up to the parent tr element before you try to find the .sub_title or .price elements. 在尝试查找.sub_title或.price元素之前,需要遍历到父tr元素。

$(document).ready(function() {
    $(document).delegate('.amount input[type="text"]','keyup',(function() {
        var $this = $(this);
        var $tr = $this.closest("tr");
        var sub_total = $tr.find('.sub_total');
        var price = $tr.find('.price').text();
        var amount = $this.val();
        alert(price);
    }));
});

It's because price and sub_total are not children of the current element (as find is selecting): 这是因为pricesub_total不是当前元素的子元素(如find所选择):

var sub_total = $this.find('.sub_total');
var price = $this.find('.price').text();

they are siblings of its parent or more simply children of the container tr . 它们是其容器tr父级或更简单的子级的同级。

Try instead: 请尝试:

var sub_total = $this.closest('tr').find('.sub_total');
var price = $this.closest('tr').find('.price').text();

example for a 一个例子

<table id="#myTable">

I write this: 我这样写:

function setCell( idTab, row, col, text) {
  var idCel = idTab + " tbody tr:nth-child(" + row + ") td:nth-child(" + col + ")";
  $(idCel).html(text);
}

function getCell( idTab, row, col ) {
  var idCel =idTab + " tbody tr:nth-child(" + row + ") td:nth-child(" + col + ")";
  return $(idCel).html();
}

setCell( "#myTab", 3, 4, "new value" );
alert( getCell("#myTab", 3, 4);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM