gpt4 book ai didi

javascript - knockout 不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:47:51 25 4
gpt4 key购买 nike

我是网络编程的新手,正在尝试在我使用 MVC 4 Razor 构建的网站上实现 Knockout Contact 表单。我直接从 Knockout 的网站上拿了这个例子,并在 JSFiddle 中测试了我的更改;一切都很好。但是当我将代码放入我的 cshtml 时,它不会获取 Knockout 代码。我对发生的事情一无所知。有什么帮助吗?细节将是有益的,因为有很多我不知道。在 cshtml 上的 Knockout 联系人网格之后有一个 BeginForm 帮助程序。这是个问题吗?

Javascript: AddTeamMember.js

var initialData = [
{
name: "Danny", email: "LaRusso@stars.come", phone: "(555) 121-2121", dept: "Print"
},
{
name: "Sensei", email: "Miyagi@stars.com", phone: "(555) 432-3466", dept: "AMS"
}
];

var ContactsModel = function (contacts) {
var self = this;
self.contacts = ko.observableArray(ko.utils.arrayMap(contacts, function (contact) {
return { name: contact.name, email: contact.email, phone: contact.phone, dept: contact.dept };
}));

self.addContact = function () {
self.contacts.push({
name: "",
email: "",
phone: "",
dept: ""
});
};

self.removeContact = function (contact) {
self.contacts.remove(contact);
};

self.addPhone = function (contact) {
contact.phones.push({
type: "",
number: ""
});
};

self.removePhone = function (phone) {
$.each(self.contacts(), function () { this.phones.remove(phone) })
};

self.save = function () {
self.lastSavedJson(JSON.stringify(ko.toJS(self.contacts), null, 2));
};

self.lastSavedJson = ko.observable("")
};

ko.applyBindings(new ContactsModel(initialData));

CSHTML:

@{
ViewBag.Title = "Register";
}
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/knockout-2.2.0.js" type="text/javascript"></script>
<script src="~/MyJS/AddTeamMember.js"></script>

<hgroup class="title">
<h1>@ViewBag.Title.</h1>
<h2>@ViewBag.Message</h2>
</hgroup>

<div class="registerForm">
<div class='memInfoForm'>

<h2>Contacts</h2>
<div id='contactsList'>
<table class='contactsEditor'>
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Dept</th>
</tr>
<tbody data-bind="foreach: contacts">
<tr>
<td>
<input data-bind='value: name' />
<div><a href='#' data-bind='click: $root.removeContact'>Delete</a></div>
</td>
<td><input data-bind='value: email' /></td>
<td><input data-bind='value: phone' /></td>
<td><input data-bind='value: dept' /></td>
</tr>
</tbody>
</table>
</div>

<p>
<button data-bind='click: addContact'>Add a contact</button>
<button data-bind='click: save, enable: contacts().length > 0'>Save to JSON</button>
</p>

<textarea data-bind='value: lastSavedJson' rows='5' cols='60' disabled='disabled'> </textarea>

</div>


@using (Html.BeginForm("Register", "Register", FormMethod.Post, new { id = "registerForm" }))
...

最佳答案

当您的 DOM 准备就绪时,必须执行此行。

ko.applyBindings(new ContactsModel(initialData));

引用自 KO 网站:

To activate Knockout, add the following line to a block:

 ko.applyBindings(myViewModel);

You can either put the script block at the bottom of your HTML document, or you can put it at the top and wrap the contents in a DOM-ready handler such as jQuery’s $ function.

http://knockoutjs.com/documentation/observables.html

关于javascript - knockout 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22749347/

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