Javascript 如何在按钮单击jquery时停止页面重新加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33465557/
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-23 15:05:53  来源:igfitidea点击:

How to stop page reload on button click jquery

javascriptjqueryasp.net

提问by Vicky

I am using this below code for button click event using jQuery. When button is clicked the page reloads.

我使用下面的代码使用 jQuery 进行按钮单击事件。单击按钮时,页面会重新加载。

$('#button1').click(function () {
    //Code goes here
    return false;
});

回答by Goran Mottram

If your "button" is a buttonelement, make sure you explicity set the typeattribute, otherwise the WebForm will treat it as submit by default.

如果您的“按钮”是一个button元素,请确保您明确设置了该type属性,否则 WebForm 将默认将其视为提交。

<button id="button1" type="button">Go</button>

If it's an inputelement, do so with jQuery with the following:

如果它是一个input元素,请使用 jQuery 执行以下操作:

$('#button1').click(function(e){
    e.preventDefault();
    // Code goes here
});

Read more: event.preventDefault()

阅读更多:event.preventDefault()

回答by doublesharp

You can use event.preventDefault()to prevent the default event (click) from occurring.

您可以使用event.preventDefault()来阻止默认事件(单击)发生。

$('#button1').click(function(e) {
    // prevent click action
    e.preventDefault();
    // your code here
    return false;
});