jQuery 遍历和查找文本框

jQuery traversing and finding textboxes

本文关键字:文本 查找 遍历 jQuery      更新时间:2023-09-26

如果我正在循环访问表中的元素 - 比如类"pmtos"的隐藏字段 - 如何获取对表中同一单元格内文本字段(输入(的引用?

jQuery 是:

 // Loop through each hidden field, which holds the outstanding amount
 $(".pmtos").each(function () {
        var os = $(this).val();
        //
        //find text box in same cell - and populate with some value
        //
        //
 });

感谢您在使其工作时提供的任何指导。

马克

以下是编辑

之前问题的解决方案(根据要求(:

$('#allocate').click(function () {
  var recd = parseFloat( $('#pmtRecd').val() );
     
  $('input.pmtallocated').each(function() {
    var value = parseFloat( $(this).parent().prev().text() );
    this.value = (recd >= value) ? value : recd;         
    recd = recd - this.value;
    if (recd == 0) {
      return false;
    }
  });  
});

注意:这不依赖于隐藏的输入。它从第二列中的td中获取文本。

这是小提琴

回答问题后编辑

可以使用siblings('.pmtallocated')prev('.pmtallocated')来获取输入。使用 siblings() 可能是两者中更好的,因为它不依赖于直接在标记中pmtos之前出现的pmtallocated

$(this).siblings('.pmtallocated').val()

尝试

 // Loop through each hidden field, which holds the outstanding amount
 $(".pmtos").each(function () {
        var os = $(this);
        var cell = os.parent(); // gets the parent, i.e. the table cell
        var input = cell.find('input')[0];
 });

你可以使用$(this).closest('input')

看看这个。 可能适合你。

   $(".pmtos").each(function () {
            var os = $(this).val();
            var input = $(this).closest('td').find('input[type=text]');
     });