在单个 select 下拉菜单中提交带有 jquery 的 select2 表单
submitting select2 form with jquery upon single select dropdown
我有一个 select 下拉菜单。当用户制作 selection 时,我希望提交的表单带有 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();
});
});
Rails 视图:
<%= 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
});
编辑
我忘了说事件侦听器可以工作,我已经用警告消息对其进行了测试,只是没有提交表单。
$('select#country').on('select2:select', function () {
$('input#submit-me').trigger('click');
});
无法提交表单,所以就这样做了。
我有一个 select 下拉菜单。当用户制作 selection 时,我希望提交的表单带有 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();
});
});
Rails 视图:
<%= 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
});
编辑
我忘了说事件侦听器可以工作,我已经用警告消息对其进行了测试,只是没有提交表单。
$('select#country').on('select2:select', function () {
$('input#submit-me').trigger('click');
});
无法提交表单,所以就这样做了。