【问题标题】:How to Reset dropdown value on "clear search"如何重置“清除搜索”的下拉值
【发布时间】:2013-02-20 13:45:42
【问题描述】:

我在搜索表单中有一个下拉菜单和一个清除搜索按钮。我希望在单击按钮时清除下拉列表中的选定值。除了我无法清除下拉列表之外,所有包含在下面的咖啡脚本代码都有效。我怎样才能做到这一点?

<%= search_form_for @search, :builder => SimpleForm::FormBuilder do |f|%>
  <%= f.input :year_gteq, :collection =>years_options%>

  <%= f.input :year_lteq, :collection =>years_options%>

<%= f.submit "Clear Search", :name => nil, :id => :q_reset, :class => "btn" %>  

咖啡脚本

$("#q_reset").click ->
    $(".clear-fields").val('')
    $('input:checkbox').removeAttr('checked')
    $('#clear-dropdown-fields').prop('selectedIndex', 0)

这对我有用:

$('表单选择').val('')

【问题讨论】:

    标签: javascript ruby-on-rails-3 forms coffeescript


    【解决方案1】:

    您可以在表单上调用reset() 将字段返回到它们的起始值。

    或者您可以将selectedIndex 设置为0

    $('#clear-dropdown-fields').prop('selectedIndex', 0);
    

    【讨论】:

    • 感谢您的帮助。不幸的是,两者都不适合我……知道为什么吗?
    【解决方案2】:

    您对 2 个 dom 元素使用相同的 id,这就是原因

    $('#clear-dropdown-fields').prop('selectedIndex', 0)
    

    在第二个下拉列表中不起作用。您可以做的是使用类而不是 id 或者只是在表单中查找元素。您也可以使用val('') 重置该值。

    $('form select').val('')
    

    【讨论】:

    • 我把它改成了 $('.clear-dropdown-fields').prop('selectedIndex', 0) 还是不行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    • 2019-03-24
    相关资源
    最近更新 更多