简体   繁体   中英

Passing checkbox value in array if checkbox is checked

My dynamically created checkbox code is here:

$(function(){

             var currentUser =  JSON.parse(window.localStorage.getItem('customer'));
            $.ajax({ 
              type : 'GET', 
              url : LiveUrl1 + "/api/Recent/GetAllRecent?userId="+currentUser.Id,
              async : false, 
              beforeSend : function(){/*loading*/},
              dataType : 'json', 
              success : function(result){
              //console.log(result);
               var buffer="";
                $.each(result, function(index, val){ 
buffer+="<li class='ui-menu-item'><div id='ui-id-2' tabindex='-1' class='ui-menu-item-wrapper'><input type='checkbox' on-change='checkboxChanged' value="+val.Id+" style='margin-right:6px;' id='selectchkbox' class='selectchkbox' />"+val.SearchTerm+"</div></li>"; 

                  $("#Recent").html(buffer);
                });

              }

             });
            });

But, how do I pass this checkbox value in the array if the checkbox is checked?

Here you can do like this: I did using jquery.

 $(function(){ var currentUser = JSON.parse(window.localStorage.getItem('customer')); $.ajax({ type : 'GET', url : LiveUrl1 + "/api/Recent/GetAllRecent?userId="+currentUser.Id, async : false, beforeSend : function(){/*loading*/}, dataType : 'json', success : function(result){ //console.log(result); var buffer=""; $.each(result, function(index, val){ buffer+="<li class='ui-menu-item'><div id='ui-id-2' tabindex='-1' class='ui-menu-item-wrapper'><input type='checkbox' on-change='checkboxChanged' value="+val.Id+" style='margin-right:6px;' id='selectchkbox' class='selectchkbox' />"+val.SearchTerm+"</div></li>"; $("#Recent").html(buffer); }); } }); var checkedValues = new Array(); $("input[type=checkbox]").on("change",function (){ var Value = $(this).val(); checkedValues.push(Value); console.log(Value); // To check the values on console. }); }); var checkedValues = new Array(); function checkboxChanged(id) { checkedValues.push(id); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

Using javascript you can do this:

  $(function(){ var currentUser = JSON.parse(window.localStorage.getItem('customer')); $.ajax({ type : 'GET', url : LiveUrl1 + "/api/Recent/GetAllRecent?userId="+currentUser.Id, async : false, beforeSend : function(){/*loading*/}, dataType : 'json', success : function(result){ //console.log(result); var buffer=""; $.each(result, function(index, val){ buffer+="<li class='ui-menu-item'><div id='ui-id-2' tabindex='-1' class='ui-menu-item-wrapper'><input type='checkbox' on-change='checkboxChanged("+val.Id+")' value="+val.Id+" style='margin-right:6px;' id='selectchkbox' class='selectchkbox' />"+val.SearchTerm+"</div></li>"; $("#Recent").html(buffer); }); } }); var checkedValues = new Array(); var checkedValues = new Array(); function checkboxChanged(id) { checkedValues.push(id); console.log(id); } }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM