【问题标题】:How to show a alert before sending a form in Rails?如何在 Rails 中发送表单之前显示警报?
【发布时间】:2019-02-18 17:35:26
【问题描述】:

我试图在用 ruby​​ on rails 发送表单之前显示一个确认按钮。

问题是我使用的解决方案不起作用。首先,它显示了两次警报,按两次按钮后,没有发送表单,也没有任何反应。

这是我的代码

  <%=form_for @transaction, :url => delete_duplicated_items_transactions_path(),:html=>{:class=>"form-horizontal bol-form"} do |f|%>

    <div>
      <script type="text/javascript">
        var checked = false;
        function selectAll(){
          checked = !checked;
          console.log(checked);
          $("input:checkbox").each(function(){
            $(this).attr('checked', checked);
          });
          return false;
        }
      </script>

      <table class="admin-table data-table" id="usertable">
        <thead>
          <tr>
            <th><input type="checkbox" onclick="selectAll()">Select All</a></th>
            <th>Bill of Lading Name</th>
            <th>Product Lot id</th>
          </tr>
        </thead>
        <tbody id="bill_of_ladings_body">
          <% duplicated.each do |element|%>
            <tr id="row_user_<%=element["product_lot"].id%>">
              <td><%= check_box_tag "product_lots[]", "#{element["bill_of_lading"].id.to_s};#{element["product_lot"].id.to_s}" %></td>
              <td><%=link_to element["bill_of_lading"].name,edit_bill_of_lading_path(:id => element["bill_of_lading"].id)%></td>
              <td><%= element["product_lot"].id %></td>
            </tr>
          <%end%>
        </tbody>
      </table>
    </div>
    <div class="admin-table data-table">
      <br>
      <td align="right">
        <%=f.button "Remove from Bill of Ladings", name: "transaction_type",value: 'remove', :class=>"btn btn-info","data-loading-text"=>"Processing..."%>      
        <%=f.button "Remove from Bill of Ladings and Recycle", name: "transaction_type", value: 'remove_and_recycle', data:{confirm:'If you proceed, the system will delete the selected Lot IDs from the selected Bill Of Ladings and will then recyle all Lot IDs added on the previous step. Any Lot ID that is not selected on this list will not be removed from the corresponding Bill of Lading/s and will not be recycled. Are you sure you want to continue?'}, :class=>"btn btn-danger" %> 

    </div>

  <% end %>

我在这里做错了什么?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3


    【解决方案1】:

    首先,您谈到警报和确认是两个不同的东西。而且你还有一个带有警报类的 div,更具体一点。

    其次,rails 的 ujs“确认”选项的正确语法是 data: {confirm: 'somestring'}},您缺少“数据:”键。

    https://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html#confirmations

    此外,在该链接上,在“确认”部分的正下方,您有一个“自动禁用”部分,因为您使用的是 data-loading-text,并且 rails 已经可以使用 data-disable-with 处理它

    【讨论】:

    • 感谢您的回答 arieljuod。但是我将(只是更新了问题)更改为您所说的,并且行为仍然相同(显示警报两次并且不发送表单)
    • 您是否在浏览器的控制台或网络选项卡上看到任何错误?同样,更准确地说,警报和确认是两个真正不同的东西,你还有一个带有“警报”类的 div。我不知道您是指确认对话框,还是在其他地方有一些警报对话框,或者您正在谈论.alert div。如果.alert.alert-block div 不相关,请将其从问题中删除(阅读关于该问题的最少代码的 SO 指南)并修正您使用的措辞。
    • 我刚刚删除了 alert.alert-block。感谢您的反馈。我在谈论确认(弹出两次但不发送表格)。 JS 控制台也没有任何消息
    • 如果您删除confirm 选项,该表单是否有效?我不知道它是否真的需要,但请尝试在按钮标签中添加 type: :submit 选项。
    • 另外,请检查您的 application.js 上是否同时具有 rails-ujs 和 jquery-ujs,也许这就是您有两个对话框的原因
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 2016-07-18
    • 2021-07-07
    • 2016-09-24
    • 1970-01-01
    • 2018-09-13
    • 1970-01-01
    相关资源
    最近更新 更多