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



說明

KnockoutJS 可觀察項 remove([值的集合]) 方法移除與給定值集合匹配的專案。

語法

arrayName.remove([set of values])

引數

此方法接受以值集合形式呈現的引數。

示例

<!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
廣告
© . All rights reserved.