代码之家  ›  专栏  ›  技术社区  ›  Axil

rails-ujs-linkto用于在特定条件下提示错误

  •  0
  • Axil  · 技术社区  · 11 年前

    这也许是个简单的问题,但我仍然找不到答案

    基本上,如果满足特定条件,我如何使用链接/按钮提示错误?

    在下面的示例中,它将在链接上提示,但您可以选择是/否。

    <%= link_to 'Void', void_invoice_path(@invoice), data: { confirm: 'Are you sure you want to void the invoice ?' } %>
    

    这不是我想要的。例如,在这种情况下,如果存在付款,则无效按钮/链接无法工作。我仍然希望允许用户单击链接,但会提示一个javascript错误。谢谢

    1 回复  |  直到 11 年前
        1
  •  0
  •   Christian Rolle    11 年前

    你必须将这种行为绑定到链接上,就像:

    <%= link_to 'Void', void_invoice_path(@invoice), 
          data: { error: 'You are not allowed to void the invoice ?' } %>
    

    在application.js中:

    jQuery(document).ready(function(){
      $('a[data-error]').click(function(event){
        alert($(this).attr('data-error'))
        $(this).preventDefault()
      })
    })
    

    就需要委托的绑定事件而言: Delegate Javascript! ...if you can.