我正在使用 Knockout.js。我有一个 HTML 表单,用户可以在其中向表格添加条目。下面是我的代码。
问题是您可以创建重复的条目。我不想允许那样。
我该如何补救?
HTML
<div class="span12">
<button style="margin-bottom: 10px;" class="btn" data-bind="click: function () { ViewModel.AddIntMember() }"><i class="icon-plus"></i>Add</button>
</div>
<div class="span8">
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th></th>
<th>Name</th>
<th>Staff No</th>
</tr>
</thead>
<tbody data-bind="foreach: ViewModel.RiskAssessment.IntTeam">
<tr>
<td>
<button class="btn btn-small" data-bind="click: function () { ViewModel.StaffViewModel.Remove($data) }">
<i class="icon-remove"></i>
Remove</button>
</td>
<td data-bind="text: Name"></td>
<td data-bind="text: StaffNo"></td>
</tr>
</tbody>
</table>
</div>
JS 相关函数
AddIntMember: function () {
LoadStaff("", 0);
$("#InternalStaffPopup").bPopup({ positionStyle: "fixed", scrollBar: true });
},
Select: function (staffMember) {
ViewModel.RiskAssessment.IntTeam.push({ Id: 0, RiskAssessmentId: 0, StaffNo: staffMember.StaffNo, Name: staffMember.Name });
},
Remove: function (staffMember) {
ViewModel.RiskAssessment.IntTeam.remove(staffMember);
},