代码之家  ›  专栏  ›  技术社区  ›  asif

当类在表单提交中相同时,button和span之间有什么区别。?

  •  2
  • asif  · 技术社区  · 8 年前

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

    这是我的span代码:

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

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

    我的问题是标记span表单提交得很好,并且得到了ajax的响应,但是当我使用按钮标记页面时,哪里是刷新的地方。

    我没有写信 <button type='submit'> 用于按钮标签

    2 回复  |  直到 8 年前
        1
  •  6
  •   arbuthnott    8 年前

    浏览器通常解释 button s在 form 如果您不告诉他们其他信息,则将其作为提交按钮。如果需要他们停止这样做,请使用:

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

    如果您知道您总是通过ajax提交表单(例如,如果您首先进行复杂的验证),您可以在表单上添加以下内容作为安全措施:

    <form onsubmit="return false;">
    

    这将阻止任何来自html的默认提交,但仍然允许您操作表单输入并在ajax调用中使用它们。

        2
  •  2
  •   Amit Kumar Singh    8 年前

    按钮是一个控件,设计用于在页面上提供事件,该事件通常完成一些操作,如提交表单字段、向页面添加新内容、导致页面组件重新排列。等等。在全球范围内,web用户将跨度的视觉表示视为内容显示,将按钮的视觉表示视为动作使能器。

    <input type="submit"> 使用 <input type="button"> 或如上述答案所述 <button type="button">