繁体   English   中英

如果选中复选框,则在数组中传递复选框值

[英]Passing checkbox value in array if checkbox is checked

我动态创建的复选框代码在这里:

$(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);
                });

              }

             });
            });

但是,如果选中该复选框,如何在数组中传递此复选框值?

在这里你可以这样做:我使用了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> 

使用javascript你可以这样做:

  $(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); } }); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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