Javascript 是否传递更新按钮的当前或更新文本字段标记值?
我有一个从会话中加载值的表 我正在处理更新按钮。我想将文本字段的当前值传递给控制器以执行更新操作 我的代码如下所示:Javascript 是否传递更新按钮的当前或更新文本字段标记值?,javascript,html,ruby-on-rails,ruby,erb,Javascript,Html,Ruby On Rails,Ruby,Erb,我有一个从会话中加载值的表 我正在处理更新按钮。我想将文本字段的当前值传递给控制器以执行更新操作 我的代码如下所示: <table> <thead> <tr> <th width="200">Name</th> <th width="150">Price</th> <th>Quantity</th> <th width="1
<table>
<thead>
<tr>
<th width="200">Name</th>
<th width="150">Price</th>
<th>Quantity</th>
<th width="150">Total</th>
<th></th>
</tr>
<% grand_total = 0 %>
<% session[:cart].each do |key, item| %>
<tr>
<td><%= item[:name] %></td>
<td>Rs. <%= item[:price] %></td>
<td><%= text_field_tag(:quantity, item[:quantity]) %>
<%= link_to("Update", {:action => "update", :id => item[:id], :quantity => item[:quantity]}) %>
</td>
<td><%= item[:total_cost] %></td>
<td>
<%= link_to("X", {:action => "delete", :id => item[:id]}) %>
</td>
</tr>
<% grand_total = grand_total + item[:total_cost] %>
<% end %>
</thead>
</table>
当我将鼠标悬停在“更新”按钮上时,路径显示localhost:3000/cart?quantity=1,即使我更改了文本字段中的值。ERB模板由服务器端的Rails呈现,但这种交互是在客户端的浏览器中发生的 是字符串插值的一种奇特形式,但您仍然可以用同样的方式来考虑它:
quantity = 1
message = "You have #{quantity} items"
quantity = 2
puts message # -> You have 1 items
你不会真的期望打印出你有两个项目,是吗
最简单的选择是在文本输入上的onchange指令中添加一些javascript,以更新提交按钮,但如果经常遇到此问题,您可能还需要研究数据绑定。作为单独的问题提问;在此处放置链接,然后稍后返回并删除评论。