开发者

jquery executes declared function without me clicking

i wonder why the browser displays "logged in" without me having clicked on the link i id-tagged with clickhere. it displays it everytime i refreshes the pa开发者_JAVA技巧ge.

<script type="text/javascript">
  $(document).ready(function(){

    function login(){
       alert("logged in");
    }

    $("#clickhere").click(login());

 });
</script>

i just want it to be displayed when i click on the link.

what is the problem?


You have to remove () after login in JQuerys click() function.

<script type="text/javascript">
  $(document).ready(function(){

    function login(){
       alert("logged in");
    }

    $("#clickhere").click(login);

 });
</script>


You have to pass the method to jQuery's click(). login() mean execute-the-login-method. You have to pass method correctly as ammoQ said.

Or you can change your code like this. It will only call login() when click the #clickhere.

<script type="text/javascript">
  $(document).ready(function(){

    function login(){
       alert("logged in");
    }

    $("#clickhere").click(function() {
        login();
    });

 });
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜