提交后,视图模型中的清除区域

时间:2013-05-18 20:37:01

标签: jquery knockout.js

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

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值的任何建议吗?

由于

2 个答案:

答案 0 :(得分:3)

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

self.AddDepartmentModel.DepartmentName(null);

而不是

$("#departmentNameTextbox").val("");
self.AddDepartmentModel.DepartmentName = null;

答案 1 :(得分:0)

很抱歉重振旧问题,但我也想提一下,你可以做以下事情:

self.AddDepartmentModel.DepartmentName(undefined);