2017-02-09 107 views
0

我有这样的代码引导复选框切换 - 如何获取复选框的值之后,点击

<link href="https://gitcdn.github.io/bootstrap-toggle/2.2.0/css/bootstrap-toggle.min.css" rel="stylesheet"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
    <script src="https://gitcdn.github.io/bootstrap-toggle/2.2.0/js/bootstrap-toggle.min.js"></script> 
    <script> 
    $('#toggle-two').bootstrapToggle({ 
     on: 'Enabled', 
     off: 'Disabled' 
    }); 
    $('[name="place[]"]').change(function() { 
     console.log(this.value) 
    }) 
    </script> 
    <input type="checkbox" name="place[]" value="USA" checked data-toggle="toggle" data-size="mini" data-onstyle="primary" data-offstyle="danger" data-on="Enabled" data-off="Disabled"> 
    <input type="checkbox" name="place[]" value="Canada" data-toggle="toggle" data-size="mini" data-onstyle="primary" data-offstyle="danger" data-on="Enabled" data-off="Disabled"> 
    <input type="checkbox" name="place[]" value="Japan" checked data-toggle="toggle" data-size="mini" data-onstyle="primary" data-offstyle="danger" data-on="Enabled" data-off="Disabled"> 
    <input type="checkbox" name="place[]" value="Russia" data-toggle="toggle" data-size="mini" data-onstyle="primary" data-offstyle="danger" data-on="Enabled" data-off="Disabled">   

我想要得到的值被点击复选框后,它是否被选中或不。

显示上面的代码,它不起作用。我正在使用bootstrap-toggle。

请帮忙。谢谢:)

回答

1

写里面$(document).ready()脚本像下面

<script> 
$(document).ready(function(){ 
    $('#toggle-two').bootstrapToggle({ 
     on: 'Enabled', 
     off: 'Disabled' 
    }); 
    $('[name="place[]"]').change(function() { 
     console.log(this.value) 
    }) 
}); 
</script> 
+0

感谢。有用 :) – JSmith

0

是这样的..

$('[name="place[]"]').change(function(){ 
    if($(this).prop("checked") == true){ 
     //checked 
    }else{ 
     //not checked 
    } 
});