2

将部门提交到服务器保存后,如何清除视图模型中部门名称的值?这是我的代码:

self.AddDepartmentModel.AddDepartment = function () {
    self.errors = ko.validation.group(this, { deep: true, observable: false });
    if (self.AddDepartmentModel.errors().length == 0) {
        $.ajax({
            url: "/Department/Add/",
            type: 'post',
            data: ko.toJSON(self.AddDepartmentModel),
            contentType: 'application/json',
            success: function (result) {
                $('#success').html('Department Added Successfully.');
                $("#success").dialog({
                    dialogClass: 'noclose',
                    autoOpen: true,
                    show: "blind",
                    hide: "explode",
                    modal: true,
                    open: function(event, ui) {
                        setTimeout(function() {
                            $('#success').dialog('close');
                        }, 3000);
                    }
                });
                self.ListOfDepartments.GetListOfAllDepartments();
                $("#departmentNameTextbox").val("");
                self.AddDepartmentModel.DepartmentName = null;
            }
        });
    } else {
        self.AddDepartmentModel.errors.showAllMessages();
        return;
    }
};

我希望在按下提交按钮后清除该字段。如果我只是将字段值设为空字符串并再次点击提交,它会在我清除它后提交文本框中的值。有什么建议如何清除 DepartmentName 的值?

谢谢

4

2 回答 2

3

我假设 self.AddDepartmentModel.DepartmentName 是一个可观察的属性,因为它绑定到一个文本框并且您获得了用户输入的值。如果是这种情况,请尝试

self.AddDepartmentModel.DepartmentName(null);

代替

$("#departmentNameTextbox").val("");
self.AddDepartmentModel.DepartmentName = null;
于 2013-05-18T20:42:39.433 回答
0

很抱歉恢复旧问题,但我还想提一下,您可以执行以下操作:

self.AddDepartmentModel.DepartmentName(undefined);

于 2017-03-27T13:56:26.643 回答