button with no postback?

后端 未结 5 1099
傲寒
傲寒 2021-02-05 02:33

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?

相关标签:
5条回答
  • 2021-02-05 03:06

    you need to use return false

    <button onclick="printElement('content');return false">Print</button>
    
    0 讨论(0)
  • 2021-02-05 03:10

    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

    0 讨论(0)
  • 2021-02-05 03:21

    Add type="button" to your <button> tag like so:

    <button type="button" onclick="printElement('content');">Print</button>
    

    This answer explains the behavior.

    0 讨论(0)
  • 2021-02-05 03:22

    if you don't need a post back you should use an HTML button.

    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)

    0 讨论(0)
  • 2021-02-05 03:25

    Alter you click event as below:

    <button onclick="printElement('content');return false;">Print</button>
    
    0 讨论(0)
提交回复
热议问题