【发布时间】:2015-06-11 12:11:34
【问题描述】:
目前我正在制作一个非常基本的时间表。 所以我目前拥有的是一个点击添加员工的按钮,它会为他们生成一周的时间。我希望发生的是员工姓名输入是一个下拉列表,但我不知道如何继续,这是我到目前为止所拥有的 小提琴 http://jsfiddle.net/grahamwalsh/1p3nnkyg/
HTML
<div class='timesheet'>
<form action='/someServerSideHandler'>
<p>You have asked for <span data-bind='text: employees().length'> </span> employee(s)</p>
<table data-bind='visible: employees().length > 0'>
<thead>
<tr>
<th>Employee Name</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
<th>Sunday</th>
<th />
</tr>
</thead>
<tbody data-bind='foreach: employees'>
<tr>
<td><input class='required' data-bind='value: name, uniqueName: true' /></td>
<td><input class='required number' data-bind='value: hours, uniqueName: true' /></td>
删除
<button data-bind='click: addEmployee'>Add Employee</button>
<button data-bind='enable: employees().length > 0' type='submit'>Submit</button>
</form>
淘汰赛
var EmployeeModel = function(employees) {
var self = this;
self.employees = ko.observableArray(employees);
self.addEmployee = function() {
self.employees.push({
Name: "",
Monday: "",
Tuesday:"",
Wednesday:"",
Thursday:"",
Friday:"",
Saturday:"",
Sunday:""
});
};
self.removeEmployee = function(employee) {
self.employees.remove(employee);
};
self.save = function(form) {
alert("Could now transmit to server: " + ko.utils.stringifyJson(self.employees));
};
};
var viewModel = new EmployeeModel([
]);
ko.applyBindings(viewModel);
$("form").validate({ submitHandler: viewModel.save });
body { font-family: arial; font-size: 14px; }
.timesheet { padding: 1em; background-color: #EEEEDD; border: 1px solid #CCC; max-width: 655px; }
.timesheet input { font-family: Arial; }
.timesheet b { font-weight: bold; }
.timesheet p { margin-top: 0.9em; margin-bottom: 0.9em; }
.timesheet select[multiple] { width: 100%; height: 8em; }
.timesheet h2 { margin-top: 0.4em; font-weight: bold; font-size: 1.2em; }
.timesheet table, .liveExample td, .liveExample th { padding: 0.2em; border-width: 0; }
.timesheet td input { width: 13em; }
tr { vertical-align: top; }
.timesheet input.error { border: 1px solid red; background-color: #FDC; }
.timesheet label.error { display: block; color: Red; font-size: 0.8em; }
.timesheet th { font-weight: bold; }
li { list-style-type: disc; margin-left: 20px; }
【问题讨论】:
-
类似 jsfiddle.net/1p3nnkyg/6 的东西。欢呼
-
@Liz 顺便说一句 - 我只是注意到到目前为止您还没有接受任何问题的答案。 It would be great if you could do so,这样您的问题就不会再显示为“未回答”;它使其他人更容易区分谁仍然需要帮助,谁不需要。谢谢!
-
嗨,对不起,我一定会这样做,我该如何接受?
-
谢谢!单击您希望接受的答案分数下方的小复选标记。 Here is a screenshot。此外,当您编写 cmets 时,您可以将某人的姓名包含在 @ 符号中,例如@janfoeh。然后,该人会收到您已回复他们的通知 - 否则他们可能会错过您的回复。干杯!
标签: knockout.js