[英]KnockoutJS submit binding not working with foreach
Hello I am unable use knockout 'submit' binding with 'foreach' binding. 您好,我无法使用带有'foreach'绑定的敲除'submit'绑定。 I cannot figure out what is mistake here. 我无法弄清楚这是什么错误。 Please help me find my mistake. 请帮助我找到我的错误。
My view model is like this: 我的视图模型是这样的:
function poReceivingModel(){
var self = this;
var initial_row = new poReceivingRowModel();
self.rows = ko.observableArray([initial_row]);
self.saveAndAdd = function(formElement){
alert('entered into function');
var row = new poReceivingRowModel();
self.rows.push(row);
};
};
function poReceivingRowModel(){
var self = this;
self.building = ko.observable();
self.isele_abc = ko.observable();
self.isele_num = ko.observable();
self.isele_floor = ko.observable();
};
And my html binded to 'viewmodel' is like this: 和我的HTML绑定到'viewmodel'是这样的:
<tbody data-bind="foreach: rows">
<form data-bind="submit: $parent.saveAndAdd">
<tr>
<td>
<!-- input field here -->
</td>
<td>
<!-- input field here -->
</td>
<td>
<!-- input field here -->
</td>
<td>
<!-- input field here -->
</td>
<td>
<button type="submit">Save and Add</button>
</td>
</tr>
</form>
</tbody>
The problem is when I click on 'Add and Save' button 'saveAndAdd' function from 'poReceivingModel' is not called. 问题是,当我单击“添加并保存”按钮时,未调用“ poReceivingModel”中的“ saveAndAdd”功能。 I do not get any alert message. 我没有收到任何警报消息。 I tried calling that function with 'click' binding on 'button' element. 我尝试在“按钮”元素上使用“单击”绑定来调用该函数。 The function get called this way ie I get alert message. 该函数以这种方式调用,即我收到警报消息。
UPDATE: In firebug I see form tag is closed just after and button is out of 'form' tag. 更新:在萤火虫中,我看到窗体标记紧随其后,并且按钮不在“窗体”标记中。
I found that form tag cannot be used inside table tag. 我发现表格标签内不能使用表格标签。 Solution is table tag can be put inside form tag . 解决方法是将表格标签放在表格标签中。 If we do not want to do so we can change the table tag to div but this may not give you desired output. 如果我们不想这样做,可以将table标记更改为div,但这可能无法提供所需的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.