Javascript 按钮没有回发?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4096170/
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
button with no postback?
提问by Peter
Well i have a asp.net page where i have a button that im using to execute a JavaScript... but after the JavaScript has run the page reloads(postback) how can i avoid that?
好吧,我有一个 asp.net 页面,我有一个按钮,我用来执行 JavaScript...但是在 JavaScript 运行后页面重新加载(回发)我怎么能避免这种情况?
<button onclick="printElement('content');">Print</button>
回答by anishMarokey
you need to use return false
你需要使用 return false
<button onclick="printElement('content');return false">Print</button>
回答by anishMarokey
Add type="button"
to your <button>
tag like so:
像这样添加type="button"
到您的<button>
标签中:
<button type="button" onclick="printElement('content');">Print</button>
This answerexplains the behavior.
这个答案解释了这种行为。
回答by Paddy
Alter you click event as below:
改变你点击事件如下:
<button onclick="printElement('content');return false;">Print</button>
回答by Massimiliano Peluso
if you don't need a post back you should use an HTML button.
如果您不需要回帖,您应该使用 HTML 按钮。
in that case you don't need override the behavior to avoid the post back and it's laighter to load (it doesn't need to be worked by the server to render back the HTML)
在这种情况下,您不需要覆盖行为来避免回发,并且加载起来更轻松(服务器不需要工作来呈现 HTML)
回答by Sandor de Klerk
You should just use an input button instead:
您应该只使用输入按钮:
<input type="button" onclick="printElement('content')" value="Print" />
this doest NEED an form (so you can use it without) and you dont have the problem with the postback.
这不需要一个表单(所以你可以不用它)并且你没有回发问题。
Hope this helps
希望这可以帮助