jQuery onClick 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5697058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:39:43  来源:igfitidea点击:

jQuery onClick event

jqueryinputonclick

提问by Earl Higgins

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

似乎无法让这个工作。任何帮助都会很棒。我有点像 jquery 的初学者。

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

Here is the HTML

这是 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.

看起来很简单,但点击输入什么都不做。是否有不同的方法来定位输入?谢谢。

回答by David Tang

Make sure you include jQuery before your script:

确保在脚本之前包含 jQuery:

<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();
    });
});

回答by SLaks

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

您应该添加return false;以防止浏览器的默认操作。

回答by Daniel A. White

Return falsefrom your function.

false从你的函数返回。

回答by webtech

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

问题可能是您的 jQuery 位于表单上方。尝试将其移动到 HTML 下方。