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

时间:2017-02-11 06:15:46

标签: javascript checkbox

我动态创建的复选框代码位于:

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

              }

             });
            });

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

1 个答案:

答案 0 :(得分:0)

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