我有这个嵌套表格:
<div class="nested-fields">
<div class="row">
<div class="col-sm-3">
<%= select_tag "ingredients", "", class: 'ingredients'%>
</div>
<div class="col-sm-3">
<%= number_field_tag "quantity", id: "quantity"%>
</div>
<div class="col-sm-2">
<%= f.label :price, id: 'price' %>
</div>
<div class="col-sm-2">
<%= f.label :total_amount, id: "total_amount", readonly: true %>
</div>
<div class="col-sm-2">
<%= link_to_remove_association "Remove", f, class: "btn btn-sm btn-danger" %>
</div>
</div>
</div>
当我输入数量值时,它将计算总行数。
document.addEventListener("turbolinks:load", function() {
$('#quantity').on('keyup',function(){
var quantity = $(this).val();
var price = $('#price').val();
var total = (quantity*price)
$('#total_amount').html(total);
});
});
“数量”字段为小数:
t.decimal :quantity, :decimal, :precision => 8, :scale => 2
但是当我在数量字段中输入值时,会在控制台上收到:
指定的值“ {:id = \ u003E \” quantity \“, :input_html = \ u003E {:value = \ u003E \“ 1.00000000 \”}}“无效 数。该值必须匹配以下正则表达式: -?(\ d + | \ d +。\ d + |。\ d +)([eE] [-+]?\ d +)?
为什么会这样?我该如何解决?
答案 0 :(得分:0)
number_field_tag(名称,值=无,选项= {})
您需要像下面那样更正number_field_tag
<%= number_field_tag "quantity", nil, id: "quantity" %>
此外,您不能通过定义为标签来提取price
和total
的值。您需要将它们定义为输入字段
<%= f.text_field :price, id: 'price' %>
<%= f.text_field :total_amount, id: "total_amount", readonly: true %>