如何仅在单击按钮时对菜单项执行操作

时间:2015-12-03 13:00:01

标签: jquery

我的代码片段中有一个登录页面,如果我们点击登录按钮,会弹出一个对话框。假设我只想在选择下拉菜单中的特定项目并单击对话框的保存按钮时导航到特定页面。有没有办法检查菜单中是否选择了特定项目,并且只有在单击按钮时才应执行与菜单项关联的操作。提前谢谢。

<script src="./Signin Template for Bootstrap_files/ie10-viewport-bug-workaround.js"></script>
<script>
    $(document).ready(function () {
        $('#btn1').click(function () {
            $("#dialog-form").dialog("open");
        });

        //code for dialog
        $("#dialog-form").dialog
        ({
            autoOpen: false,
            height: 300,
            width: 350,
            modal: true,
            zIndex: 1,
            buttons: {
                "Save": function () {
                    if ($("#name").val() == "" || $("#name").val().length < 5) {
                        $("#name").addClass("error");
                        $("#name").val('');
                    }
                    if ($("#password").val() == "" || $("#password").val().length < 5) {
                        $("#password").addClass("error");
                        $("#password").val('');
                    }
                    if ($("#email").val() == "" || $("#email").val().length < 5) {
                        $("#email").addClass("error");
                        $("#email").val('');
                    }

                    $('.dropdown-menu>li:nth-child(1)').click(function () {

                        window.location.href = "http://stackoverflow.com";
                    });

                },
                Cancel: function () {
                    $(this).dialog("close");
                    $("#name").val('');
                    $("#password").val('');
                    $("#email").val('');
                }
            }
        });
    });
</script>

  
<!DOCTYPE html>
<!-- saved from url=(0040)http://getbootstrap.com/examples/signin/ -->
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="http://getbootstrap.com/favicon.ico">

    <title>Signin Template for Bootstrap</title>

    <!-- Bootstrap core CSS -->
    <link href="./Signin Template for Bootstrap_files/bootstrap.min.css" rel="stylesheet">

    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <link href="./Signin Template for Bootstrap_files/ie10-viewport-bug-workaround.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="./Signin Template for Bootstrap_files/signin.css" rel="stylesheet">

    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]>
    <script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
    <script src="./Signin Template for Bootstrap_files/ie-emulation-modes-warning.js"></script>
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <script src="js/jquery-1.11.3.min.js"></script>
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    <script type="text/javascript"
            src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8/jquery.validate.min.js"></script>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css"/>
    <style type="text/css">
        .error {
            border-color: #F00
        }
    </style>
</head>

<body>

<div class="container">

    <form class="form-signin" id="#sign-in">
        <h2 class="form-signin-heading">Please sign in</h2>
        <label for="inputEmail" class="sr-only">Email address</label>
        <input type="email" id="inputEmail" class="form-control" placeholder="Email address" required="" autofocus="">
        <label for="inputPassword" class="sr-only">Password</label>
        <input type="password" id="inputPassword" class="form-control" placeholder="Password" required="">

        <div class="checkbox">
            <label>
                <input type="checkbox" value="remember-me"> Remember me
            </label>
        </div>
        <button class="btn btn-lg btn-primary btn-block" type="button" id="btn1">Sign in</button>
    </form>

</div> <!-- /container -->
<div id="dialog" title="Dialog Form"></div>
<div id="dialog-form" title="Create new user">
    <form id="register" name="register" action=" " method=" ">

        <label for="name">Name</label>
        <input type="text" name="name" id="name" required="true"/>
        <br/>
        <label for="email">Email</label>
        <input type="text" name="email" id="email" required="true" email="true"/>
        <br/>
        <label for="password">Password</label>
        <input type="password" name="password" id="password"/>
        <label><input type="checkbox" value="">Male</label>
        <label><input type="checkbox" value="">Female</label>

        <div class="dropdown">
            <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown"
                    aria-haspopup="true" aria-expanded="true">
                Dropdown
                <span class="caret"></span>
            </button>
            <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
                <li><a href="#">ECE</a></li>
                <li><a href="#">AEEE</a></li>
                <li><a href="#">CSE</a></li>
                <li role="separator" class="divider"></li>
                <li><a href="#">First year</a></li>
            </ul>
        </div>
    </form>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

试试这个:

    jQuery(document).ready(function($) {
       //remove the selected class from the element 
       $('.abc a').removeClass('selected')
       $('.abc a').click( function(){
          //add the class to the selected element  
          $(this).addClass('selected')
       })

       $('#btn1').click(function(){
            var selectedLocation = $('.abc a.selected').attr('href')
            window.location = selectedLocation
       })
   });

你的下拉列表中没有任何位置,但我想你很清楚!

相关问题