Disable form auto submit on button click

こ雲淡風輕ζ 提交于 2019-11-26 18:29:24

Buttons like <button>Click to do something</button> are submit buttons.

Set type="button" to change that. type="submit" is the default (as specified by the HTML recommendation).

It is not hard to do what you want. You could just try using return false (return false overrides default behaviour on every DOM element) like that :

myform.onsubmit=function()
{ 
  //do what you want;
  return false;
}

and then submit your form using myform.submit()

or alternatively :

mybutton.onclick=function () 
{
   //do what you want;
   return false;
}

However I think Input type="button"/Button type="button" will not submit your form and you could use them without any problems.

<button>'s are in fact submit buttons, they have no other main functionality. You will have to set the type to button.
But if you bind your event handler like below, you target all buttons and do not have to do it manually for each button!

$('form button').on("click",function(e){
    e.preventDefault();
});

if you want to add directly to input as attribute, use this

 onclick="return false;" 

<input id = "btnPlay" type="button" onclick="return false;" value="play" /> 

this will prevent form submit behaviour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!