【问题标题】:bootstrap html click button without submit form没有提交表单的引导html单击按钮
【发布时间】:2013-02-08 12:22:19
【问题描述】:

这是我的代码

        <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>             

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

不只是一个按钮

 type=submit

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

【问题讨论】:

  • 我对引导程序不熟悉,但是如果在添加行后创建add_user_group() 函数return false; 会发生什么?
  • @AnthonyClark 哇。它有效(不会触发表单提交)!

标签: javascript html forms button twitter-bootstrap


【解决方案1】:

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

【讨论】:

  • 这应该是真正的解决方案。另一个答案不会通过在表单字段中按 enter 来避免表单提交。
  • 此解决方案只为您提供样式输入,并为您提供有限的样式选项。
【解决方案2】:

在名为onclick 的函数中返回false 将阻止表单提交。 :)

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

【讨论】:

  • 使用按钮标签的类型来像
猜你喜欢
  • 2010-10-11
  • 1970-01-01
  • 2012-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多