如何重置“清晰搜索”上的下拉值

How to Reset dropdown value on "clear search"

本文关键字:清晰搜索 何重置 清晰 搜索      更新时间:2023-09-26

我在搜索表单中有一个下拉菜单和一个清晰的搜索按钮。我希望在下拉菜单中选择的值在单击按钮时被清除。下面包括的coffeescript代码,除了我不能得到下拉清除。我该怎么做呢?

<%= 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 (")

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

也可以将selectedIndex设置为0

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

两个dom元素使用相同的id,这就是为什么

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

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

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