jQuery onClick event

2019-04-06 21:56发布

Cant seem to get this working. Any help would be great. I am somewhat of a beginner with jquery.

$('#add_button').click(function () {
    $("#add_button").hide();
});

Here is the HTML

<input id='add_button' type='submit' class='add_now' value='' />

Seems simple enough, but clicking on the input does nothing. Is there a different method for targeting inputs? Thanks.

4条回答
Luminary・发光体
2楼-- · 2019-04-06 22:11

Make sure you include jQuery before your script:

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

And put your code inside a document-ready function:

$(document).ready(function () {
    $('#add_button').click(function () {
        $("#add_button").hide();
    });
});
查看更多
Deceive 欺骗
3楼-- · 2019-04-06 22:29

Return false from your function.

查看更多
Explosion°爆炸
4楼-- · 2019-04-06 22:30

You should add return false; to prevent the browser's default action.

查看更多
Emotional °昔
5楼-- · 2019-04-06 22:32

The problem might be that your jQuery is above the form. Try to move it below your HTML.

查看更多
登录 后发表回答