bootstrap html点击按钮没有提交表格

icn*_*icn 7 html javascript forms button twitter-bootstrap

这是我的代码

        <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>             
Run Code Online (Sandbox Code Playgroud)

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

不是只有一个按钮

 type=submit
Run Code Online (Sandbox Code Playgroud)

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

Mau*_*ger 43

无需使用Javascript.只是设置type="button"为覆盖隐式type="submit".这也可以在HTML5规范中找到,如下所述:https://stackoverflow.com/a/9643866/2175370

  • 这应该是真正的解决方案。另一个答案不能避免通过在表单字段中按Enter提交表单。 (2认同)

Ant*_*ark 14

已经false在调用该函数返回onclick将阻止表单提交.:)

onclick="javascript:add_user_group(2);return false;"
Run Code Online (Sandbox Code Playgroud)

  • 使用按钮标记的类型来像&lt;button type =“ button” onclick =“ Javascript:myFunction()”&gt;另一个任务&lt;/ button&gt;一样按钮 (2认同)