2016-12-09 67 views
0

我想获取多个复选框值,我尝试了一下,我无法得到为什么这里意味着选择和选项不存在,我试图这样做,但我无法获得值如何在没有选择标签的情况下获得多个值

<div class="dropdown-menu dropdown-menu" id="property_type2" name="property_type2" role="menu" style="color:#333;width:315px;height: 102px;border-radius:0px;" > 
 
\t <div class="chkbox" style="padding-left:5px;"> 
 
\t \t <label class="checkbox-inline checkbox-success"> 
 
\t \t <input type="checkbox" id="Check1" value="flat"> Flat 
 
\t \t </label> 
 
\t \t 
 
\t \t <label class="checkbox-inline"> 
 
\t \t <input type="checkbox" id="Check2" value="villa"> House/Villa 
 
\t \t </label> 
 
\t \t 
 
\t \t <label class="checkbox-inline"> 
 
\t \t <input type="checkbox" id="Check3" value="plot"> Plot 
 
\t \t </label> 
 
\t \t 
 
\t </div> 
 
</div> 
 

 
    var foo = []; 
 
\t \t $('.chkbox :selected').each(function(i, selected){ 
 
\t \t foo[i] = $(selected).text(); 
 
\t \t }); 
 
     console.log(foo);

+0

可能还有其他的问题,但可以肯定你的选择.chkbox不匹配的任何元素。将class ='chkbox'添加到复选框元素的html,然后重试。 –

回答

3

这是你在找什么 - https://jsfiddle.net/sekrars9/

$("#btnGetValues").click(function() { 
 
    
 
     var foo = []; 
 
     $('input[type="checkbox"]:checked').each(function() { 
 
     foo.push($(this).val()); 
 
     }); 
 
     alert(foo.join(",")); 
 
    })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<div class="dropdown-menu dropdown-menu" id="property_type2" name="property_type2" role="menu" style="color:#333;width:315px;height: 102px;border-radius:0px;"> 
 
     <div class="chkbox" style="padding-left:5px;"> 
 
     <label class="checkbox-inline checkbox-success"> 
 
      <input type="checkbox" id="Check1" value="flat"> Flat 
 
     </label> 
 
    
 
     <label class="checkbox-inline"> 
 
      <input type="checkbox" id="Check2" value="villa"> House/Villa 
 
     </label> 
 
    
 
     <label class="checkbox-inline"> 
 
      <input type="checkbox" id="Check3" value="plot"> Plot 
 
     </label> 
 
     <input type="button" id="btnGetValues" value="Get Values"> 
 
     </div> 
 
    </div>

0

下面的HTML将工作根据自己的需要:

<html> 
<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 


<script type="text/javascript"> 
$(document).ready(function(){ 
     $.each($('input'),function(){ 

     if($(this).is(':checked')) 
      console.log($(this).val()); 
     }); 
}); 
</script> 
</head> 

<body> 
<div class="dropdown-menu dropdown-menu" id="property_type2" name="property_type2" role="menu" style="color:#333;width:315px;height: 102px;border-radius:0px;" > 
    <div class="chkbox" style="padding-left:5px;"> 
     <label class="checkbox-inline checkbox-success"> 
      <input type="checkbox" id="Check1" value="flat"> Flat 
     </label> 

     <label class="checkbox-inline"> 
      <input type="checkbox" id="Check2" value="villa"> House/Villa 
     </label> 

     <label class="checkbox-inline"> 
      <input type="checkbox" id="Check3" value="plot"> Plot 
     </label> 

    </div> 
</div> 

</body> 

</html> 
相关问题