目录

remove('value')

描述 (Description)

KnockoutJS Observable remove('value')方法删除与'value'匹配的项,并作为数组返回。

语法 (Syntax)

arrayName.remove('value')

参数 (Parameters)

接受一个参数作为要删除的值。

例子 (Example)

<!DOCTYPE html>
   <head>
      <title>KnockoutJS ObservableArray remove method</title>
      <script src = "https://ajax.aspnetcdn.com/ajax/knockout/knockout-3.1.0.js"
         type = "text/javascript"></script>
   </head>
   <body>
      <p>Example to demonstrate remove() method.</p>
      <button data-bind = "click: removeEmp">Remove selected Emp</button>
      <p>Array of employees:</p>
      <select multiple = "true" size = "8" data-bind = "options: empArray , 
         selectedOptions: chosenItem"> </select>
      <script>
         function EmployeeModel() {
            this.empName = ko.observable("");
            this.chosenItem = ko.observableArray("");
            this.empArray = ko.observableArray(['Scott','James','Jordan','Lee',
               'RoseMary','Kathie']);
            this.removeEmp = function(chosenItem) {
               if (this.chosenItem().length == 0)  {
                  alert("Please select item/s to be removed.");
               }
               while(this.chosenItem().length > 0) {
                  this.empArray.remove(this.chosenItem()[0]);
               }
            }
         }
         var em = new EmployeeModel();
         ko.applyBindings(em);
      </script>
   </body>
</html>

输出 (Output)

让我们执行以下步骤来查看上述代码的工作原理 -

  • 将以上代码保存在array-remove.htm文件中。

  • 在浏览器中打开此HTML文件。

  • 从列表中选择要删除的项目,然后单击“删除所选的Emp按钮”。

新页面打开
↑回到顶部↑
WIKI教程 @2018