javascript - Can a Knockout observable be data bound to the value of a radio button? - Stack Overflow

admin2025-04-20  0

Is it possible to bind a Knockout observable property to a radio button using a value binding?

Here's what I'm trying to do, but the value ends up being the string "[Object object]" instead of the actual instance of my observable property:

<input type="radio" name="vehicleGroup" data-bind="checked: vehicleGroupViewModel().selectedGroupOption , value:vehicleGroupViewModel().car" />

<input type="radio" name="vehicleGroup" data-bind="checked: vehicleGroupViewModel().selectedGroupOption , value:vehicleGroupViewModel().truck" />

Here's the view models I'm using:

var VehicleGroupViewModel = function(){
    var self = this;
    this.selectedVehicleGroup = ko.observable();
    this.selectedGroupOption = ko.observable();
    this.selectedGroupOption.subscribe(function (newVal) {
        self.selectedVehicleGroup(newVal);
    }
    this.selectedGroup = ko.observable();
    this.car = ko.observable(new VehicleViewModel());
    this.truck = ko.observable(new VehicleViewModel());
}

var VehicleViewModel = function(){
    this.name = ko.observable();
}

So in the end I would like either the Car or Truck VehicleViewModel to be in the selectedVehicleGroup observable.

Is it possible to bind a Knockout observable property to a radio button using a value binding?

Here's what I'm trying to do, but the value ends up being the string "[Object object]" instead of the actual instance of my observable property:

<input type="radio" name="vehicleGroup" data-bind="checked: vehicleGroupViewModel().selectedGroupOption , value:vehicleGroupViewModel().car" />

<input type="radio" name="vehicleGroup" data-bind="checked: vehicleGroupViewModel().selectedGroupOption , value:vehicleGroupViewModel().truck" />

Here's the view models I'm using:

var VehicleGroupViewModel = function(){
    var self = this;
    this.selectedVehicleGroup = ko.observable();
    this.selectedGroupOption = ko.observable();
    this.selectedGroupOption.subscribe(function (newVal) {
        self.selectedVehicleGroup(newVal);
    }
    this.selectedGroup = ko.observable();
    this.car = ko.observable(new VehicleViewModel());
    this.truck = ko.observable(new VehicleViewModel());
}

var VehicleViewModel = function(){
    this.name = ko.observable();
}

So in the end I would like either the Car or Truck VehicleViewModel to be in the selectedVehicleGroup observable.

Share Improve this question edited Apr 6, 2012 at 13:37 KodeKreachor asked Apr 6, 2012 at 13:31 KodeKreachorKodeKreachor 8,89210 gold badges49 silver badges64 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

As documented here only Select nodes have the ability to bind an arbitrary JavaScript object to a value. Other inputs require a string value, which is why your value is returning "[Object object]".

You can still do what you want but you will have to manually map a key and find the appropriate object yourself. Here is a fiddle that demonstrates:

http://jsfiddle/jearles/JcPXy/

FYI for other readers ing to the accepted answer, KO v3 added the checkedValue binding which now makes this possible.

I needed this too. My solution was similar to John Earles except I used a puted instead of subscribe:

self.selectedVehicleGroup = ko.puted(function(){
    return ko.utils.arrayFirst(self.availableGroups(), function (group) { return group.name() == self.selectedGroupOption(); });
});

http://jsfiddle/JcPXy/91/

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745115287a285823.html

最新回复(0)