添加准备好我的代码

时间:2014-07-02 15:59:51

标签: javascript knockout.js onready

我试图制作一个允许用户提交问题的列表。我使用淘汰赛,我可以让它做我想要它做的事情,但当我尝试在微软视觉工作室调试时,它不会按照我想要的方式工作。当我调试时,页面打开与小提琴相同,除了"测试问题"问题列表中缺少。您也可以输入添加问题文本框,但是当您点击提交时,它会清除并且不会添加到问题列表

我被告知我需要添加一个onready,但我还是新学习如何编码并且不确定

A.怎么做 B.把它放在哪里

这是我的小提琴

http://jsfiddle.net/grahamwalsh/rCB9V/

这是我的代码

IssueList(html)

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Issue List</title>
<script src="Scripts/jquery-2.1.1.js"></script>
<script src="Scripts/knockout-3.1.0.js"></script>
<script src="Issuelist.js"></script>
<link type="text/css" rel="stylesheet" href="Issuelistcss.css" />
</head>
<body>
<div class='issuelist'>

<form data-bind="submit:addIssue">
    Add Issue: <input type="text" data-bind='value:issueToAdd, valueUpdate: "afterkeydown"' />
    <button type="submit" data-bind="enable: issueToAdd().length > 0">Add</button>
</form>

<p>Your Issues:</p>
<select multiple="multiple" height="5" data-bind="options:allIssues,   selectedOptions:selectedIssues"> </select>

<div>
    <button data-bind="click: removeSelected, enable: selectedIssues().length >  0">Remove</button>
    <button data-bind="click: sortIssues, enable: allIssues().length > 1">Sort</button>
</div>

</div>
</body>
</html>

的CSS

body { font-family: arial; font-size: 14px; }
.issuelist { padding: 1em; background-color: #87CEEB; border: 1px solid #CCC; max-width: 655px; }
.issuelist input { font-family: Arial; }
.issuelist b { font-weight: bold; }
.issuelist p { margin-top: 0.9em; margin-bottom: 0.9em; }
.issuelist select[multiple] { width: 100%; height: 8em; }
.issuelist h2 { margin-top: 0.4em; }

JS

var Issuelist = function () {
this.issueToAdd = ko.observable("");
this.allIssues = ko.observableArray(["test"]);
this.selectedIssues = ko.observableArray(["test"]);

this.addIssue = function () {
if ((this.issueToAdd() != "") && (this.allIssues.indexOf(this.issueToAdd()) < 0))
    this.allIssues.push(this.issueToAdd());
this.issueToAdd("");
};

this.removeSelected = function () {
this.allIssues.removeAll(this.selectedIssues());
this.selectedIssues([]);
};

this.sortIssues = function () {
this.allIssues.sort();
};
};

ko.applyBindings(new Issuelist());

1 个答案:

答案 0 :(得分:1)

使用jQuery在页面准备就绪时运行函数:

$(document).ready(function(){
  //Code goes here
}