i want send "true/false" value click handler, depending if checkbox checked or not.
- on check, should send 3rd parameter (ischecked) value "true".
- on uncheck, should send 3rd parameter (ischecked) value "false".
i'm sure easy, i'm having hard time figuring out. here input element:
<input class="cards-view--item-checkbox pull-right" type="checkbox" data-bind="value: universalparcelid, checked: $parent.ischecked, checkedvalue: true, click: function(data, event, ischecked) { return $root.addupidtoarray(data, event, $parent.ischecked()) }">
click handler:
addupidtoarray: function (data, event, ischecked) { var self = this; self.ischecked = ko.observable(); // if checked if(ischecked()) { self.upidarray.push(data.universalparcelid); self.upidwithindexarray.push({ universalparcelid: data.universalparcelid, searchresultindex: data.searchresultindex }); // if unchecked } else if(!ischecked()) { // remove array } return true; // allow default "click" action, checking box "check" },
i thought use "event" parameter, reason coming through jquery.event, instead of regular dom event. decided 3rd parameter. doesn't work this: gives error $parent.ischecked not function
any ideas?
unless need distinguish click other way of setting variable in checked
binding, don't want click handler. want subscribe
variable, execute function whenever value changes.
you've written click
binding if adding parameter parameter list make knockout know pass it. you'll want re-think that. generally, it's best write click handler member of viewmodel , make binding click: methodname
.
below example of click binding on checkbox. there's interval toggling checkbox each second. won't trigger click binding.
there subscription counts times value has changed, , last value was.
vm = { box: ko.observable(true), changes: ko.observable(0), lastchange: ko.observable(''), stuff: ko.observablearray(), dothing: function() { vm.stuff.push(vm.box() ? 'checked' : 'not'); return true; } }; vm.box.subscribe(function(newvalue) { vm.changes(vm.changes() + 1); vm.lastchange(newvalue ? 'checked' : 'not'); }); ko.applybindings(vm); // change checkbox without firing click setinterval(function() { vm.box(!vm.box()); }, 1000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> <input type="checkbox" data-bind="checked: box, click: dothing" /> <div>changes: <span data-bind="text:changes"></span>, last:<span data-bind="text:lastchange"></span> <div data-bind="foreach:stuff"> <div data-bind="text: $data"></div> </div>
Comments
Post a Comment