Rails 5, Ajax, jQuery -不能将接收到的数据发送到特定的元素
Rails 5, Ajax, jQuery - Cant send received data to particular element
所以,我有部分的地方是一个表行和不同的表数据。通过单击按钮(这是在<tr>
)我发送Ajax请求(简单地说,与remote: true
),也更新项目数据(显示在<tr>
)与行动。
一切都很好,但是我不能从Ajax发送接收到的数据到特定的<td>
。如果显示更多的项目-所有这些都接收到该数据。
_partial.html.er b:
<body>
<div>
<tr data-line-item-id='<%= line_item.id %>' class='line-item'>
<td data-th="Product">
<div class="row">
<div class="col-sm-2 hidden-xs"></div>
<div class="col-sm-10">
</div>
</div>
</td>
<td data-th="Price"><%= number_to_currency(line_item.product.price) %></td>
<td data-th="Quantity">
<%= line_item.quantity %>
</td>
<td data-th="Subtotal" class="text-center" class="subtotal">
<%= number_to_currency(line_item.total_price, :unit => "€", separator: ",", format: "%n %u") %></td>
<td class="actions" data-th="">
<td><%= link_to "-", decrease_line_item_path(product_id: line_item.product), remote: true %></td>
<td><%= link_to "+", increase_line_item_path(product_id: line_item.product), remote: true %></td>
</td>
</tr>
</div>
</body>
decrease.js.erb :
$("td[data-th='Quantity']").html('<%= @line_item.quantity %>');
我的"善意尝试"是这样的:
$(this).parent().parent().find("td[data-th='Quantity']").html('<%= @line_item.quantity %>');
但我的"最好的成就"是只更新特定的,按数字找到的项目,像这样:
$("td[data-th='Quantity']").eq(0).html('<%= @line_item.quantity %>');
否则它们都会被更新…
这,有点使它工作,但如果我增加一个项目的数量,然后增加其他项目的数量,首先点击显示以前更新的项目数量…但答案很接近。希望有人能帮忙…多谢
increase.js.erb :
$(document).ready(function(){
$('.increase').click(function(){
var element = $(this).parent().parent()
$(element).find("td[data-th='Quantity']").html('<%= @line_item.quantity %>');
});
});
你不应该在js中添加click事件。在您的情况下,当页面加载increase.js时。erb的点击功能不可用。
假设当前数量为2。
现在第一次点击AJAX请求被发送,在响应$('.increase')
函数被加载,但它不会执行,因为第一次点击发生在AJAX响应之前。
def增加#数量增加到3。
结束刚刚加载的增量函数将有一个新的增量数。
// first click function loaded not triggered.
$(document).ready(function(){
$('.increase').click(function(){
var element = $(this).parent().parent()
// $(element).find("td[data-th='Quantity']").html('<%= @line_item.quantity %>');
$(element).find("td[data-th='Quantity']").html('3');
// but as the click is not triggered in first click, the value has changed to 3.
});
});
在第二次单击增加按钮时,执行第一次单击函数,将td cell
的值更改为3。并且响应AJAX的第二次点击增加按钮的调用,一个$('.increase').click()
被加载值4。但是这个新加载的函数不会执行。
// second click function loaded not triggered in response to second click.
$(document).ready(function(){
$('.increase').click(function(){
var element = $(this).parent().parent()
// $(element).find("td[data-th='Quantity']").html('<%= @line_item.quantity %>');
$(element).find("td[data-th='Quantity']").html('4');
// but as the click is not triggered in second click, the value has changed to 4.
});
});
// this 2nd function will get executed on third click.
decrease.js.erb
$('tr[data-line-item-id]').each(function(){
if ($(this).data('line-item-id') == '<%= @line_item.id %>' ){
$(this).each(function(){
if ( $(this).data('th') == 'Quantity' ) {
$(this).html('<%= @line_item.quantity %>');
}
});
}
});
如果为每个tr
添加id属性,则可以简化很多,例如:
<tr data-line-item-id='<%= line_item.id %>' id='row-<%= line_item.id %>' class='line-item'>
---
</tr>
,然后在 reduce .js中访问它。erb像这样:
$('#row-<%= @line_item.id %> td:nth-child(3)').html('<%= @line_item.quantity %>');
相关文章:
- 基于数据元素限制动态表单字段
- 为什么DTM数据元素被调用两次
- 方法来使用jQuery选择匹配的数据元素
- 访问jQuery中的SVG数据元素
- Ajax 数据元素未拾取
- 从 json 字符串中提取数据元素
- Jquery验证插件,使用选项的数据元素作为验证检查
- 未捕获的异常:每个数据元素都必须实现一个唯一的“id”属性slick.dataview.js
- Knockout foreach 循环迭代相同的数据元素
- 从数据表重新加载 javascript 数据元素 asp.net 而不重新加载页面
- 如何从html中删除数据元素
- 使用jquery从HTML5数据元素中获取并执行函数+参数
- 按属性选择d3.js数据元素
- jQuery过滤器不返回数据元素,只返回整个对象
- 添加< >标记在选择的enter()部分的主数据元素之前
- 为数据元素设置一个变量
- 在javascript中转义数据元素中的撇号
- 如何访问CSV文件的数据元素
- 在adobedtm数据元素中寻找自定义脚本
- 问题更新Adobe分析变量通过数据元素在动态标签管理器- DTM