【问题标题】:How to change an input value after clicking on a link_to?单击link_to后如何更改输入值?
【发布时间】:2014-05-28 15:56:49
【问题描述】:
<input class="code_rabais" id="coupon_code" name="rabais" type="text">
<div class="ajout_rabais"><%= link_to "add", '#', :id => 'add_coupon' %></div>

我想在用户单击“添加”后将 code_rabais 的输入值重置为“”。最简单的方法是什么?

【问题讨论】:

    标签: html ruby-on-rails input link-to


    【解决方案1】:

    你可以这样做:

    link_to "add", '#', id: 'add_coupon', onclick: "$('#coupon_code').val('');return false;"
    

    另一种方法是在link_to的点击事件上添加一个事件监听器:

    link_to "add", '#', :id => 'add_coupon'
    
    # javascript in the same view 
    $('#add_coupon').on('click', function(event) {
      $('#coupon_code').val('');
      event.preventDefault();
    })
    

    【讨论】:

    • 我认为,第二种方式更干净一些。您可能应该将event.preventDefault() 添加到第二个解决方案中,这将阻止# 在地址路径中显示。
    猜你喜欢
    • 2022-01-21
    • 2021-11-25
    • 2012-07-23
    • 2018-05-18
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    相关资源
    最近更新 更多