javascript - How to get the elements selected of checkbox with knockout -


<div id="title" data-bind="foreach: list">              <input type="checkbox" data-bind="attr:{id: $index,value: list.id}" class="k-checkbox">             <label class="k-checkbox-label" data-bind="attr:{for: $index},checked: myfunction(this),text: list.id"></label>             </div>             <span data-bind="text: elementsselected"></span>         </div> 

myfunction check if selected checkbox. elementsselected elements selected of 'list'. print array span.

you can use ko.purecomputed show data based on condition.

here example:

var simplelistmodel = function(id, item) {    var self = this;    self.id = ko.observable(id);    self.item = ko.observable(item);    self.isselected = ko.observable(false);    self.printitem = ko.purecomputed(function() {      if(self.isselected()) return self.id() + " " + self.item();      else return "";    }, this);  };    var mastervm = (function () {    var self = this;          self.lists = ko.observablearray();        for(var = 0; i<5; i++) {      self.lists.push(new simplelistmodel(i, "item no. " + i));    }  })();    ko.applybindings(mastervm); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>    <div data-bind="foreach: lists">    <div>      <span>check show item: </span>      <input type="checkbox" data-bind="checked: isselected" />      <span data-bind="text: printitem"></span>    </div>  </div>


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -