0
如何使用複選框從多選中獲取所選項目名稱?我如何將它保存爲Meteor js中的記錄,因爲它可能是一個數組。我從一個網站獲得了這個漂亮的多選複選框。我如何從下拉列表中選擇選中的項目/名稱?使用複選框從多選中獲取所選項目名稱
<template name="AddSchoolLayout">
<div class="form-group" style="padding-bottom: 40px;">
<div class="dropdown-container" id="educationascope" style="width: 100%;">
<div class="dropdown-button noselect" id="educ_scope">
<div class="dropdown-label" id="ed_scope">Educational Scope</div>
<div class="dropdown-quantity">(<span class="quantity">Any</span>)</div>
<i class="fa fa-filter"></i>
</div>
<div class="dropdown-list" id="scope_edu" style="display: none; width: 100%;">
<input type="search" id="search_edu" placeholder="Search/Select Educational Scope" class="dropdown-search form-control" style="width: 100%;" />
<ul></ul>
</div>
</div>
</div>
<script>
$('#educationascope')
.on('click', '#educ_scope', function() {
$('#scope_edu').toggle();
})
.on('input', '#search_edu', function() {
var target = $(this);
var search = target.val().toLowerCase();
if (!search) {
$('li').show();
return false;
}
$('li').each(function() {
var text = $(this).text().toLowerCase();
var match = text.indexOf(search) > -1;
$(this).toggle(match);
});
})
.on('change', '[type="checkbox"]', function() {
var numChecked = $('[type="checkbox"]:checked').length;
$('.quantity').text(numChecked || 'Any');
});
// JSON of School curriculum for demo purposes
var eduscope = [
{ name: 'Scotish', abbreviation: 'SC'},
{ name: 'Nigerian', abbreviation: 'NG'},
{ name: 'Britsh', abbreviation: 'BR'},
{ name: 'American', abbreviation: 'AM'},
{ name: 'Canadian', abbreviation: 'CA'},
{ name: 'Mexican', abbreviation: 'WY' }
];
// <li> template
var schoolTemplate = _.template(
'<li>' +
'<input name="<%= abbreviation %>" type="checkbox">' +
'<label for="<%= abbreviation %>"><%= capName %></label>' +
'</li>'
);
// Populate list with states
lodash.each(eduscope, function(s) {
s.capName = lodash.startCase(s.name.toLowerCase());
$('ul').append(schoolTemplate(s));
});
</script>
</template>
這是事件類
Template.AddSchoolLayout.events({
'submit .addnewschool': function (event, template) {
event.preventDefault();
var selectedvalues = $('input[type=checkbox]:checked').map(function(_, el) {
return $(el).val();
}).get();
Meteor.call('SchoolRegister', selectedvalues,
function (error, response) {
if (error) {
return false;
}else{
FlowRouter.redirect('/contact');
}
});
}
});