Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在我的RubyonRails应用程序中使用jQueryAjax来刷新和编辑文本字段_Javascript_Jquery_Ruby On Rails_Ruby_Ajax - Fatal编程技术网

Javascript 在我的RubyonRails应用程序中使用jQueryAjax来刷新和编辑文本字段

Javascript 在我的RubyonRails应用程序中使用jQueryAjax来刷新和编辑文本字段,javascript,jquery,ruby-on-rails,ruby,ajax,Javascript,Jquery,Ruby On Rails,Ruby,Ajax,我的ruby-on-rails应用程序是为一家建筑公司开发的,其中有两个模型,一个叫做Item(rubro),另一个叫做Budget。创建的每个项目都有自己的价格。在我的预算表单中,我可以选择使用嵌套表单添加不同的项目及其数量。在第一列中,我有一个集合\u select,它与items id一起选择要添加的项目,在第二列中,您需要填写数量,在第三列中,我的想法是显示小计值(项目价格*数量) 我的问题是如何获取商品id?在集合中选择了该商品id?选择用于获取商品价格并在小计文本字段中显示(商品价格

我的ruby-on-rails应用程序是为一家建筑公司开发的,其中有两个模型,一个叫做Item(rubro),另一个叫做Budget。创建的每个项目都有自己的价格。在我的预算表单中,我可以选择使用嵌套表单添加不同的项目及其数量。在第一列中,我有一个集合\u select,它与items id一起选择要添加的项目,在第二列中,您需要填写数量,在第三列中,我的想法是显示小计值(项目价格*数量)

我的问题是如何获取商品id?在集合中选择了该商品id?选择用于获取商品价格并在小计文本字段中显示(商品价格*数量)?

有可能吗?我知道我可以使用ajax和jquery来实现,但我对这两种技术非常陌生。 或者有人能给我一些其他的方法吗

这是我表格的一部分,我想做我说过的事情: \u form.html.erb

 <%= f.fields_for :budget_details, :wrapper => false do |form| %>
                <tr class="fields ">
                  <td class="col-md-4">
                    <div class="col-md-9"><%=form.collection_select(:rubro_id, Rubro.all, :id, :name, {prompt: 'Seleccionar'}, class: 'form-control tooltip-required')%></div>
                    <div class="col-md-1"><%= link_to "<i class=\"fa fa-plus\"></i>".html_safe, new_rubro_path, {:class => "btn btn-sm btn-flat bg-green ", :title => "Nuevo Rubro"} %> </div></td>
                  <td class="col-md-1"> </td>
                  <td class="col-md-2"> </td>
                  <td class="col-md-1"><%=form.number_field :quantity, class: 'form-control tooltip-required'%></td>
                  <td class="col-md-2"><%=form.number_field :subtotal, class: 'form-control', :readonly => true%></td>
                  <td class="col-md-1"><%=form.number_field :utility, class: 'form-control'%> </td>
                  <td class="col-md-1"><%=form.link_to_remove '<i class="fa fa-trash"></i>'.html_safe, class: 'btn btn-danger'%></td>
                  <br>
                </tr>
            <% end %>
还有我的rubro_controller.rb

 def rubros_price
    @rubro = Rubro.find(params[:data])
  end
我在这里写的是伪代码,如果你能在这里提供你的代码,我也可以帮你。 请阅读下面的每一行,如果您需要更清楚的话,请发表您的评论

根据您的反馈,我将ajax代码放在上面:
在什么背景下“理解”它?客户端?服务器端?请同时粘贴一些代码。具体来说,您是如何创建表单的。另外,到目前为止,您在jquery/javascript中尝试了什么?感谢您回答@umishra。你能回答得更具体些吗?你能给我看一个更具体的ajax伪代码吗?嘿,@EmilioCenturión我已经按照你的要求编写了ajax代码。但我不确定这是否会起作用,因为我只是在假设的情况下写的。请让我知道你还需要什么。如果你愿意分享你的代码库,那么我可以更好或者更准确地帮助你。@emiliocenturion你的问题与计算小计价格有关,这是我为你写的。现在,如果你想得到更多的答案,那么你必须问一个新问题。这有意义吗?谢谢你的帮助@umishra
 def rubros_price
    @rubro = Rubro.find(params[:data])
  end
Select your item from the select box as you said from "collection_select".
 Call the ajax for item price which you selected or you can take from your page if price presents anywhere.
  You will get the price in ajax response.
 Now you have "Item", "quantity" and "price"(from ajax response).
You can calculate subtotal (price * quantity) and fill text_box.
$('#collection_select').change(function() {
  var item = this.val();
  var data = {
    'item_id': this.val();
    }
  $.ajax({
    type:"GET",
    url : "/item_price", /*Here in controller you can find price as per item id*/
    data : data,
    success : function(response) {
       var quantity = $("#item_quantity").val();
       var price = response.price;
       var subtotal_price = (quantity * price);
       $("#subtotal_price").val(subtotal_price);
    },
    error: function() {
        alert('Error occured');
    }
 });
});