jquery 引导复选框中的Onchange事件

vhmi4jdf  于 2023-03-07  发布在  jQuery
关注(0)|答案(4)|浏览(220)

我正在尝试拦截和处理 Bootstrap 复选框中的更改,并添加了“onchange”事件。很遗憾,我没有成功。目前,我有一个按钮可以更改复选框的状态,该按钮正在工作。
任何提示都将不胜感激。

<!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>
jmp7cifd

jmp7cifd1#

文档要求使用jQuerychange

    • 演示**:
$('#chk1').change(function() {
  alert($(this).prop('checked'))
})
<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">
liwlm1x9

liwlm1x92#

它对所有复选框都有效,如下例所示:

$('input:checkbox').change(function() {
  $('#console-event').html('Toggle: ' + $(this).prop('checked'));
  console.log("Change event: " + this.id);
})
<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>
    • 编辑:**

添加如下代码:

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

krcsximq3#

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

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

uqdfh47h4#

如果你不想使用jquery,你可以简单地在运行onChange函数时添加一个事件监听器,如下所示:

onChange={(事件)=〉{警报(“复选框已选中”)}}

相关问题