移除([值的集合]) 方法



说明

KnockoutJS 可观察项 remove([值的集合]) 方法移除与给定值集合匹配的项目。

语法

arrayName.remove([set of values])

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

参数

此方法接受以值集合形式呈现的参数。

示例

<!DOCTYPE html>
   <head>
      <title>KnockoutJS ObservableArray remove multiple 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([set of values]) method.</p>
      <button data-bind = "click: removemultiEmp">Remove multiple Emps</button>
      <p>Array of employees: <span data-bind = "text: empArray()" ></span></p>

      <script>
         function EmployeeModel() {
            this.empName = ko.observable("");
            this.chosenItem = ko.observableArray("");
            this.empArray = ko.observableArray(['Scott','James','Jordan','Lee',
               'RoseMary','Kathie']);

            this.removemultiEmp = function() {
               alert("This function is removing multiple matching items e.g.['RoseMary','Lee'].");
               this.empArray.removeAll(['RoseMary','Lee']);
            }
         }

         var em = new EmployeeModel();
         ko.applyBindings(em);
      </script>
      
   </body>
</html>

输出

让我们按照以下步骤了解上述代码的工作原理 −

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

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

  • 单击移除多个员工按钮。

knockoutjs_observables.htm
广告