选择单选按钮时移除成本,Ruby on rails

Removing cost when radio-button is selected, Ruby on rails

我正在 ruby 通过创建电子商务应用程序学习 rails。

我的问题是,如果客户选择了 Pick up in store 或者总金额高于 0,我该如何删除 delivery cost

因此,当 Pick up in store 或总金额大于 0 时,应删除运费。

我使用了 ifelse 来获得想要的结果。但是通过使用 Total cost 在选择 radio buttons 时不会更新。

这是没有 ifelse

的代码
    <tr>
            <th><p>Products Total: </p></th>
            <th><p>Delivery Cost: </p></th>
            <th><p>Total Cost: </p></th>
    </tr>
    <tr>
      <td><p ><%= @cart.total_price_usd %></p></td>
      <td><p ><%= @del_cost_usd %> </p></td>
      <td><p ><%= @cart.total_price_usd + @del_cost_usd%></p></td>   
    </tr>

    <div>   
      <li><%= f.radio_button :pick_up, "1", checked: false, class: 'delivery-options', data: { question: "Pick up items in store" }  %> 
      <%= f.label :pick_up, "Pick up items in store" %></li>

     <li><%= f.radio_button :pick_up, "0",  checked: true, class: 'delivery-options' , data: { question: "Have items sent by mail" } %> 
     <%= f.label :pick_up, "Have items sent by mail", class: ''  %></li> 
    </div>

谁能告诉我我想要的结果。

对于有经验的人来说,这可能是简单的要点

TIA 代码女孩

我有解决这个问题的计划。

为您显示的 DOM 提供标签和交付成本值 class 以切换其显示属性:

<p class="delivery-cost-fields">Delivery Cost: </p>
<p class="delivery-cost-fields"><%= @del_cost_usd %> </p>

对于 'Pick up items in store' 单选按钮,将其定义为 'onchange',如下所示:

<%= f.radio_button :pick_up, "1", checked: false, class: 'delivery-options', data: { question: "Pick up items in store" }, onchange: "$('.delivery-cost-fields').toggleClass('hidden', true)"  %> 

对于 'Have items sent by mail' 单选按钮,将其定义为 'onchange',如下所示:

<%= f.radio_button :pick_up, "0",  checked: true, class: 'delivery-options' , data: { question: "Have items sent by mail" }, onchange: "$('.delivery-cost-fields').toggleClass('hidden', false)" %>

定义一个隐藏的cssclass:

.hidden {display: none;}

希望对您有所帮助。