1
最近、私はフロントエンドの開発を学び始めたので、この質問があまりにも愚かであれば驚かないでください。 私がしようとしているのは、テキストボックスをバインドして、それがidではなく、テキストを選択することです。ここにはjsfiddle:https://jsfiddle.net/1rtzfLr1/があります。ここでテキストを選択するノックアウトバインドテキスト
は私のHTMLです:
<div data-bind="foreach: objects()">
<input type="text" data-bind="value: type" />
<button type="button" data-bind="click: $parent.removeObject">-</button>
</div>
<div>
<select data-bind="options: types, optionsValue: 'id', optionsText: 'title', optionsCaption: 'Type...', value: itemToAdd().type"></select>
<button id="create-object-button" type="button" data-bind="click: addObject">+</button>
</div>
そして、JS:
function model() {
var self = this;
self.objects = ko.observableArray();
self.types = ko.observableArray([new Type(1, 'one'), new Type(2, 'two'), new Type(3, 'three')]);
self.itemToAdd = ko.observable(new Object());
self.addObject = function() {
self.objects.push(self.itemToAdd());
self.itemToAdd(new Object());
};
self.removeObject = function(object) {
self.objects.remove(object);
};
function Object(type) {
var self = this;
self.type = type;
}
function Type(id, title) {
var self = this;
self.id = id;
self.title = title;
}
};
ko.applyBindings(new model());
事は、私がテキストボックスにtwo
代わりの2
を表示したいということですが、同時にこれは一部であり、私が提出したいと思っているフォームのフォームを投稿したいと思います。2
の値を実際のアプリケーションと同じようにjava enum nameに提出したいと思います。
ご質問ありがとうございました。ご迷惑をおかけいたします。