如何清除或重置输入?

时间:2014-09-04 18:08:22

标签: javascript html dom knockout.js

我尝试清除并重置输入并使用knockoutJS选择字段。下面是关于它是如何工作的片段,但为了简洁而编辑。

Javascript代码

view = (function()
{
     var self = this;
     self.anItem = ko.observable(new AnItem());
     ko.applyBindings(self)

     self.addItem = function()
     {
        self.somewhere.push(this);
        self.anItem = new AnItem(); /////this doesn't clear the form
     }
})();

function AnItem()
{
   this.Name= "";
   this.Type= 1
}

Html代码

<tfoot data-bind="with: anItem">
    <tr>
        <td><input type="text" data-bind="value= Name" /></td>
        <td><select data-bind="options: pretendThisIsPopulated, optionsValue:'Value', optionsText:'Text', value:Type" /></td>
        <td><a href="#" data-bind="click: addItem">add</a></td>
    </tr>

</tfoot>

2 个答案:

答案 0 :(得分:1)

self.anItem(new AnItem()); //should say instead

答案 1 :(得分:0)

你可以清除这些。

<强>被修改

view = (function()
{
     var self = this;

     var AnItem = function (name,type)
     {
        var $this = this;
        this.Name = ko.observable(name | "");
        this.Type = ko.observable(type | 1);
     };

     self.anItem = ko.observable(new AnItem("Name",1));
     ko.applyBindings(self)

     self.addItem = function()
     {
        self.somewhere.push(this);
        self.anItem = new AnItem("",1); 
     };

})();

使用self.addItem();清除您想要的数据。