JS档案: -
result=[];
Meteor.call('getApiResult', function (err, res) {
if (res) {
console.log("reached meteor call")
console.log(res);
result=res;
}
});
Template.dpVar.events = {
'submit .add-product-form' : function (evt) {
evt.preventDefault(); //prevent form to change URL
var form = evt.target; //this is the .add-product-form element
console.log("testing", form);
console.log(result);
for (i = 0; i < result.length; i++) {
var tempName = result[i];
console.log("form[tempName] is ");
console.log(form[tempName]);
// var tempVal = form[tempName].value
// console.log("temp name is ", tempName);
// productDB.insert({ tempName: tempVal });
// console.log("temp val is ", tempVal);
}
}
}
HTML文件: -
<template name="dpVar">
<h1>variants</h1>
<!-- here is our form -->
<form class="add-product-form">
<table class="table table-responsive table-bordered">
<tbody>
{{#each variant}}
{{#each VARIENTS}}
{{#if $eq this.DATATYPE "Text"}}
<tr>
<td class="center">{{this.NAME}}</td>
<td>
<input type="text" class="variables" name="{{this.NAME}}" value={{this.NAME}}>
</td>
</tr>
{{/if}}
{{#if $eq this.DATATYPE "price"}}
<tr>
<td class="center">{{this.NAME}}</td>
<td><input type="text" name="{{this.NAME}}" value={{this.NAME}}></td>
</tr>
{{/if}}
{{#if $eq this.DATATYPE "color"}}
<tr>
<td class="center">{{this.NAME}}</td>
<td>
<div>
<select name={{this.NAME}}>
<option>Color</option>
<option value="Green">Green</option>
<option value="White">White</option>
<option value="Red">Red</option>
<option value="Blue">Blue</option>
</select>
</div>
</td>
</tr>
{{/if}}
{{#if $eq this.DATATYPE "boolean"}}
<tr>
<td class="center">{{this.NAME}}</td>
<td><input type="radio" name={{this.NAME}}></td>
</tr>
{{/if}}
{{#if $eq this.DATATYPE "checkbox"}}
<tr>
<td class="center">{{this.NAME}}</td>
<td><input type="checkbox" name={{this.NAME}}></td>
</tr>
{{/if}}
{{#if $eq this.DATATYPE "string"}}
<tr>
<td class="center">{{this.NAME}}</td>
<td><input type="text" name={{this.NAME}}></td>
</tr>
{{/if}}
{{#if $eq this.DATATYPE "date"}}
<tr>
<td class="center">{{this.NAME}}</td>
<td><input data-provide="datepicker" type="text" name={{this.NAME}}></td>
</tr>
{{/if}}
{{/each}}
{{/each}}
</tbody>
</table>
<!-- here I added type="submit" for button, so it submits the form -->
<button class="btn btn-success addproduct" id="CreateNewProduct" type="submit">Create new product</button>
</form>
</template>
错误:
表单[tempName]未定义
我做错了什么?
我已将提交事件更改为此
Template.dpVar.events = {
'submit .add-product-form' : function (evt) {
evt.preventDefault(); //prevent form to change URL
var form = evt.target; //this is the .add-product-form element
console.log("testing", form);
console.log(result);
data = {};
for (i = 0; i < result.length; i++) {
var tempName = result[i];
if (form[tempName] !== void 0) {
var tempVal = form[tempName].value;
console.log("temp name is ", tempName);
data[tempName] = tempVal;
console.log("temp val is ", tempVal);
}
}
productDB.insert(data);
}
}
但我无法在数据库中创建条目。
{ "_id" : "urkfA4ozF4dFZihSX" }
{ "_id" : "Q8nu9eXQLPnPQSPo7" }
目前正在创建此数据库
我希望它是{&#34;品牌&#34; :Lee,&#34; Price&#34; :11}等假设结果是[品牌,价格],其值从UI
中获取