prevent refresh of page when button inside form clicked

前端 未结 13 1915
终归单人心
终归单人心 2020-11-22 04:58

I have an issue while using buttons inside form. I want that button to call function. It does, but with unwanted result that it refresh the page.

My simple code goes

相关标签:
13条回答
  • 2020-11-22 05:39

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

    if you use js do like this

    <form method="POST">
       <button name="data"  type="button" id="btnData" onclick="getData()">Click</button>
    </form> 
    
    **If you use jquery use like this**
    
    
    <form method="POST">
       <button name="data"  type="button" id="btnData">Click</button>
    </form>
    
    
    
    
    $('#btnData').click(function(e){
       e.preventDefault();
       // Code goes here
    getData(); // your onclick function call here
    
    });
    
    0 讨论(0)
提交回复
热议问题