Monday 15 July 2013

knockout.js - Getting the value of the selected Radio button in Knockout -



knockout.js - Getting the value of the selected Radio button in Knockout -

i have 3 radiobuttons so:

<input type="radio" name="ispercentordollar" value="1" data-bind="checked: ispercentordollar" /> <span>percentage amount</span> <input type="radio" name="ispercentordollar" value="2" data-bind="checked: ispercentordollar" /> <span>dollar amount</span> @html.textboxfor(p => p.discountvalue, new { @class = "form-control", @data_bind = "value: discountvalue, valueupdate: 'afterkeydown', disable: ispercentordollar() == '3'", @maxlength = "50" }) <input type="radio" value="3" class="freeshipping" name="free" data-bind="checked: ispercentordollar" /> <label>free shipping</label>

ispercentordollar used disable text box when free shipping selected.

self.ispercentordollar = ko.observable();

the viewmodel:

function couponviewmodel(data) { selfcoupon = this; self.ispercentordollar = ko.observable(); selfcoupon.discounttype = ko.observable(); selfcoupon.code = ko.observable(data.code); selfcoupon.discountvalue = ko.observable(data.discountvalue); selfcoupon.discountlevel = ko.observable("1"); };

i have set default value of discountlevel 1. remains no matter radio button clicked.

so want have value of radiobutton clicked come in discountlevel.

any clues, suggestions?

in case think looks like:

function couponviewmodel(data) { selfcoupon = this; self.ispercentordollar = ko.observable("1"); selfcoupon.discounttype = ko.observable(); selfcoupon.code = ko.observable(data.code); selfcoupon.discountvalue = ko.observable(data.discountvalue); selfcoupon.discountlevel = ko.computed(function () { homecoming self.ispercentordollar(); }); };

if want check first radio button @ beggining (quite logic me) or just:

self.ispercentordollar.subscribe(function(newvalue) { selfcoupon.discountlevel(newvalue); });

if don't want that.

this set discountlevel value of ispercentordollar, each time when ispercentordollar change.

knockout.js

No comments:

Post a Comment