我正在尝试使用bootsrap开发一个弹出式登录表单。我在弹出式窗口中有一个复选框,我无法将onClick功能强制应用到弹出式窗口内的复选框中。在弹出式窗口外,同样的功能运行良好,请建议我。
演示:Jsfiddle
整个剧本

 <script>

      $(function() {
       $('.popover-markup>.trigger').popover({
    html: true,

    title: function () {
        return $(this).parent().find('.head').html();
    },
    content: function () {
        return $(this).parent().find('.content').html();
    }
});



$('#vehicleChkBox').change(function(){

    if($(this).attr('checked')){
          $(this).val('TRUE');
     }else{
          $(this).val('FALSE');
     }

    alert($(this).val());

});

      });
    </script>

html部分
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->


  </head>

  <body>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
    <br>

    <br>
<div class="popover-markup" data-placement="bottom"> <a href="#" class="trigger" data-placement="bottom">Popover link II</a>
    <div class="head hide">Lorem Ipsum II</div>
    <div class="content hide">
    <form role="form">
                <div class="form-group">
                  <label class="control-label">E-Mail</label>
                  <input class="form-control" id="username" placeholder="hello@example.com" type="text">
                </div>
                <div class="form-group">
                  <label for="inputPassword" class="control-label">Passwort</label>
                  <input class="form-control" id="password" placeholder="******" type="password">
                </div>
                <div class="form-group">

                  <button type="button" class="btn btn-primary btn-block">Login</button>

                </div>

                <div class="row">
                  <div class="col-xs-6 col-md-8">
                    <div class="checkbox">
                      <label>
                        <input  id="vehicleChkBox" type="checkbox"> Remember me
                      </label>
                    </div>
                  </div>
                  <div class="col-xs-6 col-md-4">

                    <button type="button" class="btn btn-link  pull-right small" onClick="remember_me()">Register new Account</button>
                  </div>
                </div>
              </form>

    <div class="footer hide">test</div>

</div>

  </body>
   <script>

      $(function() {
       $('.popover-markup>.trigger').popover({
    html: true,

    title: function () {
        return $(this).parent().find('.head').html();
    },
    content: function () {
        return $(this).parent().find('.content').html();
    }
});



$('#vehicleChkBox').change(function(){

    if($(this).attr('checked')){
          $(this).val('TRUE');
     }else{
          $(this).val('FALSE');
     }

    alert($(this).val());

});

      });
    </script>

</html>

请引导我。

最佳答案

我建议使用点击。此外,还必须对body元素应用绑定。然后显式监视其中触发操作(如复选框(vehiclechbox)或按钮)的特定元素。
元素未被绑定的原因是该元素不可见。

$('body').on('click', '#vehicleChkBox', function(){
   window.alert('I was clicked');
});

更新演示:http://jsfiddle.net/D3VP9/4/

关于javascript - 弹出框内的复选框功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22420710/

10-12 14:22
查看更多