【发布时间】:2016-09-02 08:57:28
【问题描述】:
我有一个选择下拉菜单。当用户进行选择时,我希望使用 jquery 提交表单。
我的jquery代码如下:
$(document).on('page:change', function(){
function matchStart(params, data) {
params.term = params.term || '';
if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) == 0) {
return data;
}
return false;
}
$("select#user_country").select2({
matcher: function(params, data) {
return matchStart(params, data);
},
placeholder: "Select a country",
})
$("select#user_country").on('select2:select', function (event) {
return ('form#selectcountry').submit();
});
});
导轨视图:
<%= form_tag users_path, method: :get, id: 'selectcountry' do |f| %>
<%= select_tag "country", options_from_collection_for_select(ISO3166::Country.countries.sort_by(&:name), 'un_locode', 'name'), :include_blank => true %>
<%= submit_tag "Search", :name => "nil", :id => "submit-me" %>
<% end %>
Link here to 'select2:select' documentation
$('select').on('select2:select', function (evt) {
// Do something
});
编辑
我忘了提到事件监听器有效,我已经用警报消息对其进行了测试,它只是没有提交表单。
【问题讨论】:
标签: jquery jquery-select2