[英]Meteor - how to output checkbox value's with Meteor.js?
我在使用Meteor.js輸出所選復選框的值時遇到問題。 復選框值在瀏覽器中輸出為[object Object]。 有人可以幫我嗎。
HTML
<head>
<title>project</title>
</head>
<body>
{{>addStatusForm}}
</body>
<template name="addStatusForm">
<form class="addStatus">
{{#each category}}
<input type="checkbox" name="categoryCheckbox" class="boxCheck" value={{categoryDesc}}>{{categoryDesc}}<br>
{{/each}}
<input type="text" name="status">
<input type="submit" value="Add status">
</form>
{{#each status}}
<p>{{statusDesc}} {{category}}</p>
{{/each}}
</template>
助手
Template.addStatusForm.helpers({
status: function () {
return Status.find();
},
category: function(){
return Category.find();
}
});
活動
Status = new Mongo.Collection('status');
Category = new Mongo.Collection('category');
Template.addStatusForm.events({
'submit .addStatus': function (event) {
event.preventDefault();
var statusInput = event.target.status.value;
var categorySelected = $('.boxCheck:checked').val();
//var categorySelected = event.target.categoryCheckbox.value; tried this
//var categorySelectedString = JSON.stringify(categorySelected); tried this also
//console.log(categorySelected); just testing console output
//console.log(statusInput); just testing console output
Status.insert({
statusDesc : statusInput,
category : categorySelected
});
}
問題不在於它的存儲方式( console.log(typeof categorySelected )
表明它是一個字符串而不是一個對象),這是因為您在顯示數據時在此模板中兩次定義了“類別”字段,一次是從一個輔助對象,一次作為收集對象內部的一個字段。 它使幫助程序的優先級高於收集數據,因此您可以從return Category.find();
的對象return Category.find();
這是一個對象,因此[object Object]輸出
這里有兩個快速解決方案:
A)將category
助手的名稱更改為categories
或其他名稱
B)(可能更容易流線)將每個內部代碼移動到狀態模板中,這使它有點孤立,因此看不到父模板類別字段
{{#each status}}
<p>{{statusDesc}} {{category}}</p>
{{/each}}
/*...BECOMES...*/
{{#each status}}
{{>statusTemplate}}
{{/each}}
<template name="statusTemplate">
<p>{{statusDesc}} {{category}}</p>
</template>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.