optionsTextをバインドすることはできますが、optionsValueをバインドすることはできません。両方の機能は同一である必要がありますか?
レンダリングされたhtml:(価値はどこにありますか?)
<option value="">ROSSFORD D</option>
意見:
<select multiple="multiple" width="75" id="foo" name="campaign[precincts][]" data-bind="options: campaign_precincts, optionsText: function(item) {
return item.precinct_location.id
}, optionsValue: function(foo) {return foo.precinct_location.id }> </select>
モデルの表示:
var newCampaign = function() {
this.items = ko.observableArray();
this.freeText = ko.observable("");
this.campaign_precincts = ko.observableArray();
this.selectedPct = ko.observable();
this.campaignName = ko.observable();
this.userParty = ko.observable("");
self = this;
var question = this.freeText();
this.searchMe = function() {
console.log (self.userParty());
self.items([]);
self.userParty()
if (this.freeText() != "") {
// search by city
$.getJSON('/search.json?q=' + this.freeText(), function (data) {
if (data) {
console.log(data)
data.forEach(function(item) { self.items.push(item) })
}
});
// search by zipcode
$.getJSON('/search.json?z=' + this.freeText(), function (data) {
if (data) {
data.forEach(function(item) { self.items.push(item) })
}
});
}
}.bind(this);
this.addPrecinct = function(pct) {
// returs false if pct is not a member of the array
x = function(a,b){return!!~a.indexOf(b)}
if ( x(self.campaign_precincts(),pct) == false) {
self.campaign_precincts.push(pct);
};
}.bind(this);
this.removePct = function() {
self.campaign_precincts.pop(self.selectedPct());
}
};
ko.applyBindings(new newCampaign());
jsオブジェクト:
data.precinct_location.city => "string"
data.precinct_location.id => 1234