为什么删除所有和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>
答案 0 :(得分:0)
对于RemoveAll,只需清空数组
myModel.array1([]);
转移:
// this is falling over -- alert(myModel.val()); <- should be myModel.textval()
// therefore not getting to this.
myModel.array1.unshift(myModel.textval());
此外,如果您可以创建一个JSFiddle。
答案 1 :(得分:0)
您的标记和视图模型中存在问题,打开此小提琴上的浏览器控制台并查看不同操作的错误:
https://jsfiddle.net/z15d5awa/
shift1 : function(){
alert(myModel.val());
myModel.array1.unshift(myModel.textval());
},
myModel.val()
这是什么?