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

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;
    }
};


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

谢谢

最佳答案

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

self.AddDepartmentModel.DepartmentName(null);


代替

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

关于jquery - 提交后 View 模型中的 knockout 清除字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16628712/

10-14 13:05