Rails + Radio Buttons + AJAX:如何在单选按钮单击时呈现部分? [英] Rails + Radio Buttons + AJAX: How to render partial on radio button click?

查看:37
本文介绍了Rails + Radio Buttons + AJAX:如何在单选按钮单击时呈现部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个电子商务网站的结账表格.它具有用于运输选项(快递、标准等)的单选按钮,我希望它显示 <%= shipping_delivered_day %> 的动态值,以显示在 p.label_delivery_date 标记在用户使用 AJAX 单击的按钮旁边.这是我的 html:

I have a check out form for my ecommerce site. It has radio buttons for shipping options (express, standard, etc) and I want it to show a dynamic value of <%= shipping_delivered_day %> to appear inside the p.label_delivery_date tag next to the button the user clicks using AJAX. This is my html:

<% @shipping_services.each do |ship| %>
  <%
    shipping_service = ShippingService.find_by_name(ship.shipping_service)
    ship_id_value = ""+shipping_service.id.to_s
  %>
  <label class="reverse-label">
    <span class="check">
        <%= radio_button :shipment, :desired_shipping_method, ship_id_value, :checked => "checked" %>
        <%= radio_button :shipment, :desired_shipping_method, ship_id_value "%>
    </span>

    <span class="label">
      <%= shipping_service.description %>
      <p class="label_delivery_date" ></p>
    </span>
    <span class="label">
      <%= shipping_service.description %>
    </span>
  </label>

<% end %>

我知道我可以在我的单选按钮上做一个 :onclick 选项,但这只会将我发送到一个 javascript 文件.我需要能够传递我的动态 shipping_delivered_day 值,所以我需要 rails 来处理它.我将如何通过单击单选按钮发送 JS 请求?

I know I can do an :onclick option on my radio button, but that would just send me to a javascript file. I need to be able to pass in my dynamic shipping_delivered_day value, so I need rails to handle it. How would I send JS request from a radio button click?

推荐答案

Ruby/Rails 实际上提供了一种非常巧妙的方法.您所要做的就是让单选按钮在单击时调用 javascript 函数,然后将部分渲染放入该函数中,如下所示:

Ruby/Rails actually provides a pretty slick way of doing this. All you have to do is have your radio button call a javascript function on click, and then put the partial render in that function, like so:

<script>
function render_partial() {
  $('#partial-area').html('<%=j render :partial => "partial" %>')
}
</script>

<%=  radio_button :model, :field, :value, :onclick => "render_partial();" %>
<div id="partial-area"></div>

当单击单选按钮时,这会将给定的部分呈现到部分区域"div 中.对于多个部分,只需让多个函数作用于同一个 div.在您的特定实例中,您希望部分包含一些代码来确定您的发货日期.您可以使用 locals (render :partial => "ship_3day", :locals => {:p => @product}) 渲染部分并让您的部分返回适当的结果 (<代码><%= p.shipping_delivered_day %>).

This will render the given partial into the 'partial-area' div when the radio button is clicked. For multiple partials, just have multiple functions acting on the same div. In your specific instance, you'll want your partial to contain some code that will determine your shipping delivery date. You could render the partial with locals (render :partial => "ship_3day", :locals => {:p => @product}) and have your partial return the appropriate result (<%= p.shipping_delivered_day %>).

这篇关于Rails + Radio Buttons + AJAX:如何在单选按钮单击时呈现部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆