当前位置: 动力学知识库 > 问答 > 编程问答 >

html - How to prevent form submit when a button clicked?

问题描述:

This question already has an answer here:

  • How to prevent buttons from submitting forms

    13 answers

网友答案:

I don't agree with the javascript answers. They work, but they're not fixing the cause of the problem.

The form is being submitted because the <button> element has a type of submit by default (see the spec here).

You can get around this by putting type="button" in the html like so:

 <button type="button" class="btn btn-default" id="btn-area">
网友答案:

Use event.preventDefault();

clicked anchors will not take the browser to a new URL. We can use event.isDefaultPrevented() to determine if this method has been called by an event handler that was triggered by this event.

 $("#btn-area").click( function(event) {


 event.preventDefault();

     alert("hello ");

}); 

http://jsfiddle.net/BL3Jk/

网友答案:

You can try this:

$('#form-area').submit(function (event) {
    event.preventDefault();
});
网友答案:

You Can use jquery like this

$("#btn-area").click(function(event){
alert("Your alert");
event.preventDefault();
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: