Bootstrap复选框中的Onchange事件

时间:2016-07-08 06:44:16

标签: javascript jquery html checkbox

我正在尝试拦截并处理引导程序复选框中的更改,并且我添加了一个“onchange& fn;事件。不幸的是,我没有成功。目前,我有一个按钮,可以更改复选框的状态,这是有效的。

任何提示都会非常感激。

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap Toggle</title>
    <link href="./css/github.min.css" rel="stylesheet">
    <link href="./css/bootstrap.min.css" rel="stylesheet">
    <link href="./font-awesome-4.6.3/css/font-awesome.min.css" rel="stylesheet">
    <link href="./css/bootstrap-toggle.css" rel="stylesheet">
    <link href="./css/stylesheet.css" rel="stylesheet">
    <script src="./js/jquery-2.1.3.min.js"></script>
    <script src="js/bootstrap-toggle.js"></script>
    </head>
    <body>
    <div id="events" class="container">
    <h3>API vs Input</h3>
    <p>This also means that using the API or Input to trigger events will work both ways.</p>
    <div class="example">           
    <input id="chk1" type="checkbox" data-toggle="toggle" onchange="fonctionTest()">                
    <input id="chk2" type="checkbox" data-toggle="toggle">
    <input id="chk3" type="checkbox" data-toggle="toggle">
    <input id="chk4" type="checkbox" data-toggle="toggle">          
    <button class="btn btn-success" onclick="toggleOnOff('#chk1','on')">On by API</button>
    <button class="btn btn-danger" onclick="toggleOnOff('#chk1','off')">Off by API</button>
<div id="console-event"></div>
<script>
$(function() {
$('input:checkbox').change(function() {
$('#console-event').html('Toggle: ' + $(this).prop('checked'))
})
})
</script>
    <script>        
        function fonctionTest(){
        console.log("This is a test");
    }               
                function toggleOnOff(selector,state){
                    console.log("element : " + selector);
                    console.log($(selector).prop('checked'));
                    $(selector).bootstrapToggle(state); 


                }
                function toggleOn() {
                    $('#chk1').bootstrapToggle('on');
                    isOnOrOff();
                }
                function toggleOff() {
                    $('#chk1').bootstrapToggle('off');
                    isOnOrOff();
                }
                function isOnOrOff(){
                    var c=document.getElementById('chk1');
                    console.log(c.checked);
                }   
            </script>
        </div>

</body>
</html>

3 个答案:

答案 0 :(得分:12)

文档说使用jQuery change

<强>演示

&#13;
&#13;
$('#chk1').change(function() {
  alert($(this).prop('checked'))
})
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/js/bootstrap.min.js"></script>
<link href="https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css" rel="stylesheet">
<script src="https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"></script>

<input id="chk1" type="checkbox" checked data-toggle="toggle">
&#13;
&#13;
&#13;

答案 1 :(得分:4)

它适用于所有复选框,如下例所示:

&#13;
&#13;
$('input:checkbox').change(function() {
  $('#console-event').html('Toggle: ' + $(this).prop('checked'));
  console.log("Change event: " + this.id);
})
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/js/bootstrap.min.js"></script>
<link href="https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css" rel="stylesheet">
<script src="https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"></script>
<input id="chk1" type="checkbox" data-toggle="toggle">                
<input id="chk2" type="checkbox" data-toggle="toggle">
<input id="chk3" type="checkbox" data-toggle="toggle">
<input id="chk4" type="checkbox" data-toggle="toggle">
<div id="console-event"></div>
&#13;
&#13;
&#13;

修改

添加如下代码:

<script>
  $(function() {
    $('input:checkbox').change(function() {
      $('#console-event').html('Toggle: ' + $(this).prop('checked'))
    })
  })
</script>

答案 2 :(得分:1)

<input id="chk1" type="checkbox" data-toggle="toggle" onclick="fonctionTest()"> 

function fonctionTest(){
  if (document.getElementById('chk1').checked) 
  {
      alert("Checked")
  } else 
  {
      alert("Not Checked")
  }
}