gpt4 book ai didi

javascript - Knockout.js removeAll 函数不起作用

转载 作者:行者123 更新时间:2023-11-28 06:13:11 25 4
gpt4 key购买 nike

为什么removeAll和indexOf不起作用?排序、拼接、切片和反向工作正常。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type='text/javascript' src='knockout-3.4.0.js'> </script>
</head>
<body>
<table height="100" border="1" width="800">
<tr>
<td>
<span data-bind="text : array1()"></span>
</td>
</tr>
</table>
<div>
<table height="100" border="1" width="1000">
<tr>
<td><button data-bind="click : sort1" >Sort</button></td>
<td><button data-bind="click : shift1" >Shift</button></td>
<td><button data-bind="click : reverse1" >Reverse</button></td>
<td><button data-bind="click : splice1" >Splice</button></td>
<td><button data-bind="click : indexof1" >IndexOf</button></td>
<td><button data-bind="click : slice1" >Slice</button><br></td>
<td><button data-bind="click : remove1" >remove</button></td>
<td><button data-bind="click : removeAll1" >removeAll</button></td>
<td><button data-bind="click : removeFunction" >remFunction</button>/td>
</tr>
</table>
<table height="100" border="1" width="1000">
<tr>
<td>Enter Text <input data-bind="value: textval"></td></input>
</tr>
</table>
</div>

<span data-bind="text: textval"></span>
<script type="text/javascript">
var myModel={
array1 : ko.observableArray(["animal","boy","cat","dog","elf","fox"]),
textval : ko.observable(""),
sort1 : function(){
myModel.array1.sort();
},

shift1 : function(){
alert(myModel.val());
myModel.array1.unshift(myModel.textval());

},

类次工作不正常?

     reverse1 : function(){
myModel.array1.reverse();
},

splice1 : function(){
alert( myModel.array1.splice(1,3));
},
indexof1: function(){
alert(myModel.textval());
alert(myModel.array1.indexOf(myModel.textval()));
},

slice1 : function(){
alert( myModel.array1.slice(1,4));
},

removeAll1 : function(){
alert("removall");
myModel.array1.removeAll();
}

removeAll 也无法正常工作。我尝试了很多次,但在 JSP 中根本不起作用。

  }
ko.applyBindings(myModel);
</script>
</body>
</html>

最佳答案

您的标记和 View 模型存在问题,请在此 fiddle 上打开浏览器控制台并查看不同操作的错误:

https://jsfiddle.net/z15d5awa/

     shift1 : function(){
alert(myModel.val());
myModel.array1.unshift(myModel.textval());
},

myModel.val()这是什么?

关于javascript - Knockout.js removeAll 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36200019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com