如何在

我是 jQuery 的新手,我正在使用 jQuery 1.7.1 来学习 Knockout JS,我正在关注作者的视频演示。在他的示例中,他有一个类似的标签

<a href="#" class="button-delete">Delete</a>

,在视图模型中,他有类似的内容

$(document).on("click", ".button-delete", function() { console.log("inside"); });

当我在我身边尝试时,当我单击删除按钮时,我从未看到 console.log 显示在 Chrome F12 屏幕的控制台窗口上。我在这里有两个部分的问题

  1. 是否我做错了什么导致控制台消息显示?
  2. 如果我没有类来执行 css,是否有其他方法可以在视图模型中执行相同的任务?

编辑: 我纠正了我的拼写错误,代码有正确的括号(我使用网络矩阵,所以它可以解决这些问题)。这是我的 html

<!DOCTYPE html>
<script src="Scripts/jquery-1.7.1.js" type="text/javascript"></script>
<script src="Scripts/knockout-2.0.0.js" type="text/javascript"></script>
<script src="Scripts/ViewModel.js" type="text/javascript"></script>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title></title>
    <link href="assets/css/bootstrap.min.css" rel="stylesheet">
</head>
<body onload="init()">
    <input data-bind="value: tagsToAdd"/>
    <button data-bind="click: addTag">Add</button>
   <ul data-bind="foreach: tags">
           <li>
               <span data-bind="text: Name"></span>
               <div>
                   <a href="#" class="btn btn-danger" >Delete</a>
               </div>
           </li>
   </ul>
</body>
</html>

这是我的淘汰视图模型

/// <reference file="jquery-1.7.1.js" />
/// <reference file="knockout-2.0.0.js" />

var data = [
   {Id: 1, Name: "Ball Handling" },
   {Id: 2, Name: "Shooting" },
   {Id: 3, Name: "Rebounding" }
];

function viewModel()
{
    var self = this;    
    self.tags = ko.observableArray(data);
     self.tagsToAdd = ko.observable("");

    self.addTag = function() {
       self.tags.push({ Name: this.tagsToAdd() });
       self.tagsToAdd("");
    }
}


$(document).on("click", ".btn-danger", function () {
    console.log("inside");
    });


 var viewModelInstance;
function init(){
    this.viewModelInstance = new viewModel();
    ko.applyBindings(viewModelInstance);    
}

I am new to jQuery and I am using jQuery 1.7.1 to learn Knockout JS, I was following a video demo by the author. In his example he has a tag something like

<a href="#" class="button-delete">Delete</a>

and in the viewmodel he has something like

$(document).on("click", ".button-delete", function() { console.log("inside"); });

When I tried in my side when I click on the delete button I never see the console.log show up on the console window of the Chrome F12 screen. I have two part question here

  1. Is there something I am doing wrong which is preventing the console message to show up?
  2. If I do not have a class to do css, is there any other way to perform the same task in the viewmodel?

edit:
I corrected my typo, the code has proper parenthesis (I use web matrix so it take care of those issues). Here is my html

<!DOCTYPE html>
<script src="Scripts/jquery-1.7.1.js" type="text/javascript"></script>
<script src="Scripts/knockout-2.0.0.js" type="text/javascript"></script>
<script src="Scripts/ViewModel.js" type="text/javascript"></script>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title></title>
    <link href="assets/css/bootstrap.min.css" rel="stylesheet">
</head>
<body onload="init()">
    <input data-bind="value: tagsToAdd"/>
    <button data-bind="click: addTag">Add</button>
   <ul data-bind="foreach: tags">
           <li>
               <span data-bind="text: Name"></span>
               <div>
                   <a href="#" class="btn btn-danger" >Delete</a>
               </div>
           </li>
   </ul>
</body>
</html>

Here is my knockout viewmodel

/// <reference file="jquery-1.7.1.js" />
/// <reference file="knockout-2.0.0.js" />

var data = [
   {Id: 1, Name: "Ball Handling" },
   {Id: 2, Name: "Shooting" },
   {Id: 3, Name: "Rebounding" }
];

function viewModel()
{
    var self = this;    
    self.tags = ko.observableArray(data);
     self.tagsToAdd = ko.observable("");

    self.addTag = function() {
       self.tags.push({ Name: this.tagsToAdd() });
       self.tagsToAdd("");
    }
}


$(document).on("click", ".btn-danger", function () {
    console.log("inside");
    });


 var viewModelInstance;
function init(){
    this.viewModelInstance = new viewModel();
    ko.applyBindings(viewModelInstance);    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

葵雨 2025-01-13 23:47:58

您遇到任何错误吗?

您是否加载了 jQuery.jsknockout.js

请发布视图模型的代码。


啊!知道了,你有一个拼写错误

$(document).on("click", ".button-delete", function() {
//   console.log("inside";   <-- here it is
    console.log("inside");
 });

DEMO

Are you getting any errors?

Have you loaded the jQuery.js and the knockout.js

please post the code of view model.


ah! got it you have a typo

$(document).on("click", ".button-delete", function() {
//   console.log("inside";   <-- here it is
    console.log("inside");
 });

DEMO

我做我的改变 2025-01-13 23:47:58

看起来你已经得到了第一个答案。如果您没有类名,则在绑定单击事件的“其他方法”上,有几个选项。您可以向标签添加一个 id,并以这种方式调用它。或者,如果您不想添加类或 id,您可以使用带有“click”内置绑定的数据绑定语法来调用视图模型上的方法(显然这不是 jquery evnet 风格,所以这取决于你想如何连接你的事件)。像这样:

<button data-bind="click: someMethod">Click me</button>

Looks like you got your first answer already. On the "other ways" to bind the click event if you don;t have a class name, there are a few options. You could add an id to the tag, and call it that way. Or if you don't want to add a class nor an id, you can use the data-bind syntax with the "click" built-in binding to invoke a method on your view model (obviously this is not the jquery evnet style, so its up to you how you want to wire up your events). Like this:

<button data-bind="click: someMethod">Click me</button>
つ低調成傷 2025-01-13 23:47:58

奈尔首先让我知道你想在这里做什么
如果你想删除按钮有效。然后使用 jquery Ui 的删除功能,如果你想控制台某些东西,那么只需编写 console.log("you Want to console");

我认为你的行 function() { console.log("inside"; });是错误的。

Nair first let me know that what are you want to do here
if you want to delete button works. then use remove function of jquery Ui and if you want to console some thing then just write console.log("you want to console");

i think your line function() { console.log("inside"; }); is wrong .

江南月 2025-01-13 23:47:58

我建议您查看淘汰赛的点击绑定,而不是将淘汰赛与随机查询混合。单击绑定将允许您将单击事件绑定到视图模型中的函数。

I would recommend that you look at the click binding for knockout rather than mixing knockout with random query. The click binding will allow you to bind the click event to a function in the view model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文