javascript - Storing checkboxes state in current session/run -


my view including checkbox clickevent listener should fired every time checkbox state had been changed:

<html>     <head>         <script type="text/javascript" src="~/scripts/jquery.unobtrusive-ajax.js"></script>         <script type="text/javascript" src="~/scripts/jquery-1.4.4.js"></script>          <title>testchckbox</title>         <script type="text/javascript">             $(function () {                  $(document).on("click", "input.chks", function (e) {                     var _this = $(this);                     var ischecked = _this.is(':checked');                     $.post("@url.action("updateinput","search")?id=" + _this.attr("id") +                                           "&newvalue=" + ischecked, function (data) {                                               // response stuff should here                                           });          });     });  </script>       </head> 

--

       @if (viewbag.slicelist != null)         {             @*foreach(pirsum.models.hourslice slice in this.model) {*@              foreach (var slice in viewbag.slicelist) {                 <div  class="row">             <input class="chks" type="checkbox" id="@htmlhelper.generateidfromname("cb_slice." + @slice.sliceid)" />                     <label for="@htmlhelper.generateidfromname("cb_slice." + @slice.sliceid)">@slice.slicename</label>                     <span class="info">@slice.starttime - @slice.endtime</span>                  </div>             }         }               <input type="submit" value="submit" />        }             </body>         </html> 

the divs populated correctly several checkboxes , strings action passes items in viewbag view.

the problem function above isn't working , "updateinput" isn't fired in search controller when checkboxes getting clicked nor click event on client side.

what doing wrong? , how can save checkboxes state(checked , unchecked) in session or in other option can temporary save checked parameters set on runtime?

controller actions reference, 1 send data body(related checkboxes , other data matter)

public actionresult testchckbox() {     int iinstid = 1;      test.models.datalayer db = new test.models.datalayer();     test.models.testdb.context = new models.testdb();      ienumerable<test.models.hourslice> lst = db.getslices(context, iinstid).orderby(a => a.sliceid);     viewbag.slicelist = lst;     return view(lst); } 

this action should ran checkboxes state when checking/unchecking boxes:

[httppost] public actionresult updateinput(int id, bool newvalue) {     //do saving here.     return json(true); } 

you're using jquery 1.4.4; on didn't exist yet in version. need use bind or upgrade version of jquery.

other that, don't put scripts in head of html , if do, wrap them in $(document).ready(). also, it's hugely bad idea attach delegate high-level document. every click, everywhere, cause jquery query dom see if happened on object class .chks. want attach delegate absolute closest parent can away with. finally, based on code don't need delegate. use delegates when dynamically added dom , still needs trigger event handlers. if element there, page load, attach handler directly id or class.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -