简体   繁体   English

jQuery-同一行中的目标表单元格

[英]jQuery - Target Table Cell in Same Row

I have a table with a single input field and an AJAX script that runs when the input field value is modified. 我有一个带有单个输入字段的表,以及一个在修改输入字段值时运行的AJAX脚本。 This is all working well. 一切都很好。 I now need to extend this to insert a date into another cell in the same row, but now sure how to target this as the ID will have to be dynamic. 我现在需要扩展它,以便在同一行的另一个单元格中插入一个日期,但是现在要确定如何定位此日期,因为ID必须是动态的。 Here's the current table: 这是当前表:

 <table class="table table-condensed table-striped table-bordered"> <thead> <th class="text-center" scope="col">Order Number</th> <th class="text-center" scope="col">Order Date</th> <th class="text-center" scope="col">Con Note</th> </thead> <tbody> <tr> <td>123456</td> <td id="85759.OrderDate"></td> <td id="85759"><input type="text" class="form-control" placeholder="Con Note" name="conNote" value=""></td> </tr> <tr> <td>987654</td> <td id="85760.OrderDate"></td> <td id="85760"><input type="text" class="form-control" placeholder="Con Note" name="conNote" value=""></td> </tr> </tbody> </table> 

I need to insert the current data into the Order Data cell when the AJAX script is run, something like this: 运行AJAX脚本时,我需要将当前数据插入“订单数据”单元格,如下所示:

$("#85759.OrderDate").html('current date');

but not sure how to dynamically target the Order Data cell? 但不确定如何动态定位订单数据单元格? I'm setting the ID for the Order Data cell to be the same ID as the input field with ".OrderDate" appended. 我将订单数据单元格的ID设置为与输入字段相同的ID,并附加了“ .OrderDate”。 Current script is: 当前脚本是:

 $(document).ready(function() { $("input[type='text']").change(function() { var recid = $(this).closest('td').attr('id'); var conNote = $(this).val(); $this = $(this); $.post('updateOrder.php', { type: 'updateOrder', recid: recid, conNote: conNote }, function(data) { data = JSON.parse(data); if (data.error) { var ajaxError = (data.text); var errorAlert = 'There was an error updating the Con Note Number - ' + ajaxError; $this.closest('td').addClass("has-error"); $("#serialNumberError").html(errorAlert); $("#serialNumberError").show(); return; // stop executing this function any further } else { $this.closest('td').addClass("has-success") $this.closest('td').removeClass("has-error"); } }).fail(function(xhr) { var httpStatus = (xhr.status); var ajaxError = 'There was an error updating the Con Note Number - AJAX request error. HTTP Status: ' + httpStatus; $this.closest('td').addClass("has-error"); //display AJAX error details $("#serialNumberError").html(ajaxError); $("#serialNumberError").show(); }); }); }); 

You can select the cell by $this.closest('tr').children('td[id$="OrderDate"]') . 您可以通过$this.closest('tr').children('td[id$="OrderDate"]')选择单元格。

You can simplify it more by: 您可以通过以下方式进一步简化它:

  • Instead of using attribute ends with selector ( [id$=".."] ), if you can, add a CSS class "OrderDate" for example to all the order date cells, and simplify the selector to $this.closest('tr').children('.OrderData') 如果可以,不要使用选择符( [id$=".."]结尾的属性 ,例如,向所有订购日期单元格添加CSS类“ OrderDate”,并将选择器简化为$this.closest('tr').children('.OrderData')
  • Instead of closest() use parents() . 取而代之的closest()使用parents() This is a micro-optimization. 这是微优化。 The only difference is that closest tests the actual element itself for matching the selector, and in this case you know you only need to check parent elements 唯一的区别是, closest测试实际元素本身是否与选择器匹配,在这种情况下,您知道只需要检查父元素
  • You can also optionally rely on the fact that the cells are siblings and instead of children use siblings like like $this.parents('td').siblings('.OrderDate') 您也可以选择依赖于一个事实,即细胞是兄弟姐妹,而不是children使用siblings一样喜欢$this.parents('td').siblings('.OrderDate')

You can get the parent element 'tr' and then find the 'td.OrderDate', I suggest you to use a class to identify the td in the context of its parent. 您可以获取父元素“ tr”,然后找到“ td.OrderDate”,建议您使用一个类在其父上下文中标识td。

 $(function () { $("input[type='text']").change(function() { var parent = $(this).parents('tr'); // Get any element inside the tr $('td.OrderDate', parent).text('[current date]') }) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td>987654</td> <td id="85760.OrderDate" class="OrderDate"></td> <td id="85760"><input type="text" class="form-control" placeholder="Con Note" name="conNote" value=""></td> </tr> </table> 

Check the code below. 检查下面的代码。 I've removed the ajax call and replaced it with the success block, but the concept is still the same. 我删除了ajax调用,并用成功块替换了它,但是概念仍然相同。 It gets the cell that has an id that ends with "OrderDate" on the same row and sets the html for that cell. 它会获得在同一行上具有以“ OrderDate”结尾的id的单元格,并设置该单元格的html。 I've used the jQuery Ends With selector for this. 我为此使用了jQuery Ends With选择器

 $(document).ready(function() { $("input[type='text']").change(function() { var recid = $(this).closest('td').attr('id'); var conNote = $(this).val(); var $this = $(this); $this.parents('tr:first').find("td[id$='OrderDate']").html(new Date()); $this.closest('td').addClass("has-success") $this.closest('td').removeClass("has-error"); }); }); 
 .has-success { border: 1px solid green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="table table-condensed table-striped table-bordered"> <thead> <th class="text-center" scope="col">Order Number</th> <th class="text-center" scope="col">Order Date</th> <th class="text-center" scope="col">Con Note</th> </thead> <tbody> <tr> <td>123456</td> <td id="85759.OrderDate"></td> <td id="85759"><input type="text" class="form-control" placeholder="Con Note" name="conNote" value=""></td> </tr> <tr> <td>987654</td> <td id="85760.OrderDate"></td> <td id="85760"><input type="text" class="form-control" placeholder="Con Note" name="conNote" value=""></td> </tr> </tbody> </table> 

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

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