我的 HTML 中有三个元素,问题,分数,评论。分数是一个 HTML SELECT,有“差”、“好”和“优秀”三个选项。
如果 Score不是=“Good”,我只希望 Comment 字段可见。
<!-- ko foreach: questions -->
<tr>
<td data-bind="text: question"></td>
<td><select data-bind="options: availableScores"></select></td>
<td>
<!-- ko if: availableScores() != 'Good' -->
<input data-bind="textInput: comment" />
<!-- /ko -->
</td>
</tr>
<!-- /ko -->
任何建议表示赞赏,谢谢。
我假设评论文本框必须仅在所选分数与值“好”不同时才可见。
为此,必须跟踪所选值并将其绑定到列表框,下面通过属性selectedScore
.
最小的 MCVE 显示了这种行为。
var Question = function() {
_self = this;
_self.question = "";
_self.comment = ko.observable("");
_self.availableScores = ["Good", "Poor", "Excellent"];
_self.selectedScore = ko.observable("Good");
}
var vm = new Question();
ko.applyBindings(vm);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<select data-bind="options: availableScores, value: selectedScore"></select>
<!-- ko if: selectedScore() != 'Good' -->
Comment: <input data-bind="textInput: comment" />
<!-- /ko -->
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句