最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - What is the difference between button and span when the class is same in form submission.? - Stack Overflow

matteradmin7PV0评论

i have overe with span and button issue while submitting the form with ajax call.

Here is my code with button:

<form>
<button class="btn btn-round btn-fill pull-right mg-top"  id="gdBasic" >Generate</button>
</form>

Here is my code with span:

<form>
<span class="btn btn-round btn-fill pull-right mg-top"  id="gdBasic" >Generate</span>
</form>

javascript Generate Starts here.....

$("#gdBasic").click(function () {
///////////////Ajax call is inside.//////////////////
});

My problem is with tag span the form is submitting well and getting response from ajax but where as when i did it with button tag page is refreshing.

So i just want to know what is the difference between them i have not written <button type='submit'> for button tag

i have overe with span and button issue while submitting the form with ajax call.

Here is my code with button:

<form>
<button class="btn btn-round btn-fill pull-right mg-top"  id="gdBasic" >Generate</button>
</form>

Here is my code with span:

<form>
<span class="btn btn-round btn-fill pull-right mg-top"  id="gdBasic" >Generate</span>
</form>

javascript Generate Starts here.....

$("#gdBasic").click(function () {
///////////////Ajax call is inside.//////////////////
});

My problem is with tag span the form is submitting well and getting response from ajax but where as when i did it with button tag page is refreshing.

So i just want to know what is the difference between them i have not written <button type='submit'> for button tag

Share Improve this question asked Aug 26, 2017 at 10:18 asifasif 2791 gold badge3 silver badges19 bronze badges 1
  • 1 if you not define the type of button, the it is considering a submit type. – chirag satapara Commented Aug 26, 2017 at 10:21
Add a ment  | 

2 Answers 2

Reset to default 6

Browsers often interpret buttons within forms as submit buttons if you do not tell them otherwise. If you need them to stop doing this, use:

// with type button, the form will not refresh the page.
<button type="button">Will not refresh the page.</button>

If you know you are always submitting your form via ajax (for example, if you are doing plex validations first), you can add this as a safety on the form:

<form onsubmit="return false;">

That will prevent any default submissions from the html, but still allow you to manipulate the form inputs and use them in ajax calls.

span is a inline page element, a container for stuff which by default wraps around its content unless otherwise specified. button is a control which is designed to provide an event on page which generally gets some action done like submitting form fields, adding new stuff to page, causing page ponents to re-arrange. etc. Globally, web users identify with visual representation of span as content display and visual representation of button as action enabler.

If form submit is your intention and you do not want to use <input type="submit">, use <input type="button"> or as specified in answer above <button type="button">

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far