javascript - Knockoutjs 复杂绑定(bind)组合选择表

标签 javascript json knockout.js knockout-2.0

我有一个用于添加新预算详细信息的表格,如下图所示:

enter image description here

当我选择收入帐户时,另一行将添加到 View 模型集合中:

enter image description here

我想在添加新行时将所有字段值设置为“0.00”,我也遇到问题,因为如果我删除一行,则“更改"组合事件不存在,因此在更改最后一个组合时无法添加新行。

有什么线索吗?这是 fiddle 工作示例:http://jsfiddle.net/rLUyS/9/

这是我用来将更改操作绑定(bind)到最后添加的组合的代码:

$('select[name=cboincomeaccount_' + newRowIndex + ']').bind("change", {
    combo: $(this)
}, handler);

function handler(event) {

    newRowIndex++;
    var combo = jQuery(this);
    var row = combo.parent().parent();

    appViewModel.addRow();

    // Unbind
    combo.unbind('change');
    // Bind new combo
    jQuery('select[name=cboincomeaccount_' + newRowIndex + ']').bind("change", {
        combo: jQuery(this)
    }, handler)

    jQuery(row).find('input[name^="txtincmonth"]').removeAttr('disabled');    
};

提前致谢!!

最佳答案

这可能不是 knockout 问题,而是用户界面问题。

I want to set all field values to "0.00" when the new row is added

嗯,这很简单。只需将可观察行初始化为全零即可。

When I select an Income Account then another row is added to the viewmodel collection... and also I have a problem because if I delete a row then the "change" event of the combo doesnt exist so there is no way to add a new row when changing the last combo.

这可能是一个可以协商的要求。

为什么不创建一个“添加”按钮,而不是坚持这种“漂亮”的行为,当用户在下拉列表中创建一个部分时添加一行?

此外,即使我们可以完成您所要求的任务(并且我可以设想一种我们可以做到这一点的方法),当需要将用户的输入保存到服务器时您会做什么?您打算忽略最后一个空行吗?

关于javascript - Knockoutjs 复杂绑定(bind)组合选择表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13851628/

相关文章:

javascript - 如何在ajax成功时将knockout.js可见绑定(bind)设置为true?

javascript - 使用 radio 输入和 knockoutjs 选择客户地址

javascript - 创建 Firebase 用户并同时将数据添加到数据库中

javascript - JQuery 单击元素外部

javascript - 如何将 JSON 对象转换为 base64 字符串?

c# - 将 xml 字符串转换为 json - 删除 xelement 键

javascript - 使用发送时jquery文件上传不调整图像客户端

javascript setTimeout 无法识别函数参数

javascript - 无法使用 Wikipedia API 调用获取随机 JSON 对象属性

javascript - Durandal 和 Knockout 激活方法和 ctor.prototype.activate 之间的区别?