Rails: using link_to to make a link without href

后端 未结 8 1835
情书的邮戳
情书的邮戳 2020-12-29 19:05

How can I use the link_to helper to make a link without an href attribute? I want to make a link exclusively for a javascript action, I don\'t want

相关标签:
8条回答
  • 2020-12-29 19:35

    Set the href to:

    javascript:void(0)
    

    This will let you have a link that clicking it does nothing, doesn't reload the page, and doesn't move the page up to the top.

    0 讨论(0)
  • 2020-12-29 19:36
    <%= link_to '+ Add Make', 'javascript:void(0)', id: 'add-make', onclick: 'addMake()' %>
    

    and then your jQuery:

    function addMake() {
      $.ajax({
        url: '/dealers',
        type: 'GET',
        dataType: 'script',
        success: function (data) {
          console.log('it was successful')
          $('.add-make').hide();
        },
        error: function (data) {
          console.log('it was error')
        }
      });
    }
    
    0 讨论(0)
提交回复
热议问题