Javascript 我能';让最简单的knockout.js示例无法工作?
我已经尝试过此代码,但无法正常工作。我的代码在下面Javascript 我能';让最简单的knockout.js示例无法工作?,javascript,knockout.js,Javascript,Knockout.js,我已经尝试过此代码,但无法正常工作。我的代码在下面 <link href="~/Content/knocktest.css" rel="stylesheet" /> <script src="~/Scripts/knockout-2.3.0.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(funct
<link href="~/Content/knocktest.css" rel="stylesheet" />
<script src="~/Scripts/knockout-2.3.0.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
var ViewModel = function (first, last) {
this.firstName = ko.observable(first);
this.lastName = ko.observable(last);
this.fullName = ko.computed(function () {
return this.firstName() + " " + this.lastName();
}, this);
};
ko.applyBindings(new ViewModel("Planet", "Earth"));
});
</script>
$(文档).ready(函数(){
var ViewModel=函数(第一个,最后一个){
this.firstName=ko.observable(first);
this.lastName=ko.observable(last);
this.fullName=ko.computed(函数(){
返回此.firstName()+“”+此.lastName();
},这个);
};
ko.应用绑定(新视图模型(“行星”、“地球”);
});
下面是我的html代码
<p>First name: <input data-bind="value: firstName" /></p>
<p>Last name: <input data-bind="value: lastName" /></p>
名字:
姓氏:
您的代码中没有错误,因此您真正缺少的唯一一件事是您在使用jQuery库时对jQuery库的引用
$(document).ready(function () {
// rest of your code here
});
如果不包括jQuery,那么只需删除$(document).ready()
代码,并确保JavaScript在主体中的所有html元素之后
<p>First name: <input data-bind="value: firstName" /></p>
<p>Last name: <input data-bind="value: lastName" /></p>
<span data-bind="text: firstName"></span>
<span data-bind="text: lastName"></span>
<script src="http://cdnjs.cloudflare.com/ajax/libs/knockout/2.2.1/knockout-min.js"> </script>
<script>
var ViewModel = function (first, last) {
this.firstName = ko.observable(first);
this.lastName = ko.observable(last);
this.fullName = ko.computed(function () {
return this.firstName() + " " + this.lastName();
}, this);
};
ko.applyBindings(new ViewModel("Planet", "Earth"));
</script>
名字:
姓氏:
var ViewModel=函数(第一个,最后一个){
this.firstName=ko.observable(first);
this.lastName=ko.observable(last);
this.fullName=ko.computed(函数(){
返回此.firstName()+“”+此.lastName();
},这个);
};
ko.应用绑定(新视图模型(“行星”、“地球”);
请查看此您的示例代码运行良好。您是否在控制台中看到任何错误?可能您忘记附加jQuery了?您好@f_martinez,knockout-2.3.0.js,它仍然在那里,我已经检查过了it@JosephGabriel,我不能得到任何错误,所以击倒是不够的。您在代码中使用了
$
,这肯定也引用了jQuery。@Sajith很高兴我能提供帮助。