简体   繁体   中英

Simple javascript code doesn't work

I am wondering why javascript alerts doesn't popup:

asp.asp file:

<html>
<head>
<meta charset="utf-8">
<title>AAA</title>

   <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

  <script class="code" language="javascript" type="text/javascript">
    $(document).ready(function(){
            $( "#btn-statistika" ).click({
                alert('ok');
            });
            $( "#btn-lokality" ).click({
                alert('ok');
            });
    });
  </script>
</head>
<body>

<button class='ui-state-default' id='btn-statistika' style='' >Statistika</button>
<button class='ui-state-default' id='btn-lokality' style='' >Lokality</button>

</body>
</html>

Another asp files are working, I don't know why this cannot work...

You need to pass a function as param to click() call

$(document).ready(function () {
    $("#btn-statistika").click(function () {
        alert('ok');
    });
    $("#btn-lokality").click(function () {
        alert('ok');
    });
});

Here is the updated code

<html>
<head>
<meta charset="utf-8">
<title>AAA</title>

   <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

  <script class="code" language="javascript" type="text/javascript">
    $(document).ready(function(){
            $( "#btn-statistika" ).click(function(){
                alert('ok');
            });
            $( "#btn-lokality" ).click(function(){
                alert('ok');
            });
    });
  </script>
</head>
<body>

<button class='ui-state-default' id='btn-statistika' style='' >Statistika</button>
<button class='ui-state-default' id='btn-lokality' style='' >Lokality</button>

</body>
</html>

Note: You need to pass a function() in click({}); event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM