Html 没有提交表单的bootstrap html点击按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15031712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 05:50:03  来源:igfitidea点击:

bootstrap html click button without submit form

javascripthtmlformsbuttontwitter-bootstrap

提问by icn

Here is my code

这是我的代码

        <form class="form-horizontal">

          <div class="control-group">
            <div class="controls">
              <button onclick="javascript:add_user_group(2);" class="btn">add</button>
            </div>
          </div>

        <table id="users-table" class="table table-striped" >
          <thead>
            <tr>
              <th>User Name</th>
              <th>Delete</th>            
            </tr>
          </thead>
          <tbody>

          </tbody>
        </table>

      </form>             

I just want to add a new row to a table when a add button clicked, but it seems a click of the add button triggers a submission of form.

我只想在单击添加按钮时向表中添加一个新行,但似乎单击添加按钮会触发提交表单。

Isn't is only a button with

不只是一个按钮

 type=submit

to submit form. Or any button in a form triggers a submission of form ?

提交表格。或者表单中的任何按钮触发表单提交?

回答by Maurice Schleu?inger

No need to use Javascript. Just set type="button"to overwrite the implicit type="submit". This can also be found in the HTML5 specs as mentioned here: https://stackoverflow.com/a/9643866/2175370

无需使用 Javascript。只需设置type="button"覆盖隐式type="submit". 这也可以在此处提到的 HTML5 规范中找到:https: //stackoverflow.com/a/9643866/2175370

回答by Anthony Clark

Having falsereturned in the function called onclickwill prevent form submission. :)

false调用的函数中返回onclick将阻止表单提交。:)

onclick="javascript:add_user_group(2);return false;"