单击不使用 KnockoutJS
Click not working with KnockoutJS
我是 KnockoutJS 的新手,下面是我的代码。单击事件没有响应,因为按钮没有响应。有人可以告诉我哪里出错了。
我在谷歌上搜索了解决方案,但找不到任何帮助。
HTML代码:
<table class="table table-striped b-t text-sm table-bordered table-hover">
<thead>
<tr>
<th>Name</th>
<th>Mobile</th>
<th>Role</th>
<th>Created On</th>
<th>GM</th>
<th>Action</th>
</tr>
</thead>
<tbody data-bind="foreach: $data">
<tr>
<td data-bind="text: $data.user_fname + ' ' + $data.user_lname">
</td>
<button class="btn btn-danger btn-xs" data-bind="click: $data.removeUser">Delete</button>
</td>
</tr>
</tbody>
<script>
getUsers();
</script>
JS代码:
function getUsers()
{
$.ajax({
url: "api/example/func/format/json",
dataType: 'json'
}).done(function(data){
users = ko.observableArray(data);
ko.applyBindings(users);
}).error(function(jqXHR, textStatus){
alert(jqXHR.status)
});
}
function removeUser() {
console.log('userRemoved');
}
在下一行 ko.applyBindings(users);
中,您将变量 users
绑定到所有 html 文档作为上下文。当你在 html 中使用 $data 时,它实际上就是那个对象 users
。
当您执行 foreach
(您可以简单地猜测)时,您的上下文成为 users
数组中的元素之一。稍后您调用 data-bind="click: $data.removeUser"
敲除时,尝试在当前上下文中找到 removeUser
函数,即 user[i]
对象。但它不在那里,因为根据你的 js - 它是在 global score
中定义的
我是 KnockoutJS 的新手,下面是我的代码。单击事件没有响应,因为按钮没有响应。有人可以告诉我哪里出错了。
我在谷歌上搜索了解决方案,但找不到任何帮助。
HTML代码:
<table class="table table-striped b-t text-sm table-bordered table-hover">
<thead>
<tr>
<th>Name</th>
<th>Mobile</th>
<th>Role</th>
<th>Created On</th>
<th>GM</th>
<th>Action</th>
</tr>
</thead>
<tbody data-bind="foreach: $data">
<tr>
<td data-bind="text: $data.user_fname + ' ' + $data.user_lname">
</td>
<button class="btn btn-danger btn-xs" data-bind="click: $data.removeUser">Delete</button>
</td>
</tr>
</tbody>
<script>
getUsers();
</script>
JS代码:
function getUsers()
{
$.ajax({
url: "api/example/func/format/json",
dataType: 'json'
}).done(function(data){
users = ko.observableArray(data);
ko.applyBindings(users);
}).error(function(jqXHR, textStatus){
alert(jqXHR.status)
});
}
function removeUser() {
console.log('userRemoved');
}
在下一行 ko.applyBindings(users);
中,您将变量 users
绑定到所有 html 文档作为上下文。当你在 html 中使用 $data 时,它实际上就是那个对象 users
。
当您执行 foreach
(您可以简单地猜测)时,您的上下文成为 users
数组中的元素之一。稍后您调用 data-bind="click: $data.removeUser"
敲除时,尝试在当前上下文中找到 removeUser
函数,即 user[i]
对象。但它不在那里,因为根据你的 js - 它是在 global score