如何使用以下引导类从我的表单中创建的下拉列表中检索所选值?
我正在使用Meteor 0.9.2和mizzao:bootstrap-3
我的HTML:
<template name="createpost">
<form class="form-horizontal" role="form" id="createpost">
<div class="form-group form-group-lg">
<label class="col-sm-2 control-label" for="field">Field</label>
<div class="col-sm-10">
<select class="form-control">
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
<option value="four">Four</option>
<option value="five">Five</option>
</select>
</div>
</div>
<input type="submit" value="post">
</form></template>
CLIENT.JS:
Template.createpost.events({
'submit form#createpost': function(e) {
e.preventDefault();
var insertpost = {
field: //CODE TO RETRIEVE THE VALUE OF THE SELECTED ITEM //
}
Meteor.call('insertPostData', insertpost);
} });
SEVER.JS:
Meteor.methods({
'insertPostData': function(insertpost){
return insertpost._id = AllPosts.insert(insertpost);
} });
答案 0 :(得分:2)
Template.createpost.events({
'submit form#createpost': function(e, tmpl) {
e.preventDefault();
var selectedOption = tmpl.find('.form-control :selected');
var insertpost = {
field: (selectedOption && selectedOption.text)
}
}
});
答案 1 :(得分:2)
我明白了。我必须给select一个id,然后在javascript中引用这个id。我用了#34; someId&#39;对于这个例子。这很完美。谢谢你的初步帮助。
<template name="createpost">
<form class="form-horizontal" role="form" id="createpost">
<div class="form-group form-group-lg">
<label class="col-sm-2 control-label" for="field">Field</label>
<div class="col-sm-10">
<select class="form-control" id="someId>
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
<option value="four">Four</option>
<option value="five">Five</option>
</select>
</div>
</div>
<input type="submit" value="post">
client.js:
Template.createpost.events({
'submit form#createpost': function(e, tmpl) {
e.preventDefault();
var insertpost = {
field: $( "#someId" ).val();
}
Meteor.call('insertPostData', insertpost);
} });