Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

login - Twitter Bootstrap cant stop a dropdown from closing on click

I have looked at the other questions similar and tried many of the solutions, but none have worked. Here is the issue, this is a mini login form for the top banner. I need the button to not automatically close when you click in the fields. Here is my code:

    <script> 
    $(document).on('click', '.dropdown-menu', function(e){
        $(this).hasClass('keep_open') && e.stopPropagation(); // This replace if conditional.
    });
     ?</script>                 
    <div class="btn-group" >
      <a class="btn btn-small btn-inverse dropdown-toggle" data-toggle="dropdown" href="#">
        <i class="cus-key"></i> Login
        <span class="caret"></span>
      </a>
      <ul class="dropdown-menu pull-right keep_open">
        <form action="clog.php" method="post" class="keep_open">
        <!-- dropdown menu links -->
          <li><input type="text" placeholder="Username..." class="keep_open" /></li>
          <li><input type="text" placeholder="Password..." class="keep_open"/></li>
          <li><input type="submit" name="submit" style="background-image: url('img/login.png'); width: 110px; height: 32px; cursor: hand; margin-top: -5px" value=" " /></li>
          <li><a href="/riders/register.php" ><span style="color: green; float: right" > Sign up for account<i class="icon-double-angle-right"></i></a></span></li>
         </form>
      </ul>
    </div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I figured it out. It was not inside the document ready function. (hat tip to Koala_dev) Javascript needs to be:

    <script type="text/javascript">

    $(document).ready(function() {
        $(document).on('click', '.dropdown-menu', function (e) {
            $(this).hasClass('keep_open') && e.stopPropagation(); // This replace if conditional.
        }); 
    });
    </script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...