javascript - Retrieving value from select list with knockout.js -
i'm using knockout.js 2.3.0 , i'm trying retrieve value set in select list, can't work. when try alert value, either see javascript, undefined or [object object] (depending on i've tried).
the list populated fine , can set default value, can't retrieve it. doing wrong?
here's html list
<select data-bind='value: selectedmonth, options: $root.months, optionstext: "month"'></select> and here's js
self.months = [{month: '-'},{month: '01'},{month: '02'},{month: '03'},{month: '04'},{month: '05'},{month: '06'},{month: '07'},{month: '08'},{month: '09'},{month: '10'},{month: '11'},{month: '12'}]; self.selectedmonth = ko.observable(self.months[0]); self.submitbutton = function(){ alert(self.selectedmonth); //a bunch of javascript alert(self.selectedmonth()); //object, object alert(self.months[self.selectedmonth]); //undefined }
your second call 1 want:
self.selectedmonth() this unwraps observable value. it's object, being example:
{ month: "03" } this easier spot if console.log instead of alert.
in case, month value ask property:
alert(self.selectedmonth().month); see this fiddle.
Comments
Post a Comment