Bootstrap 模式中的表单

问题描述 投票:0回答:3

我有一个用于注册产品的modal。我想在模式内提交表单而不离开模式。

<!-- Button to trigger modal -->
<a href="#myModal" role="button" class="btn" data-toggle="modal">Launch demo modal</a>
 
<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Modal header</h3>
  </div>
  <div class="modal-body">
    <form method="post">
        <button type="submit">Submit</button>
    </form>
  </div>
  <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
    <button class="btn btn-primary">Save changes</button>
  </div>
</div>
twitter-bootstrap submit modal-dialog
3个回答
23
投票

就我而言,我只将提交按钮放在页脚正文中 并删除数据关闭 IE:


<div class="modal-content">
    <form id="role-form"  method="get">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>

            <h4 class="modal-title">تعديل صلاحيات المدير - {{$user->username}}</h4>
        </div>
        <div class="modal-body">

            <div class="form-group col-md-12">

                <select id="role" name="role" class="form-control">
                    <option selected disabled>الصلاحية</option>
                    <option value='1'>مدير</option>
                    <option value='2'>مشرف</option>
                </select>
            </div>

            <div class="clearfix"></div>
        </div>
        <div class="modal-footer">

            <button type="submit" class="btn btn-success" >حفظ</button>
            <button type="button" class="btn btn-default" data-dismiss="modal">إلغاء</button>

        </div>
    </form>
</div>

15
投票

首先,在您的

<form>
标签上设置一个 id:

<form id="myForm" method="post">
    ...
</form>

如果您使用 jQuery(强烈推荐),您可以这样做:

$(function(){
    $('#myForm').on('submit', function(e){
      e.preventDefault();
      $.post('http://www.somewhere.com/path/to/post', 
         $('#myForm').serialize(), 
         function(data, status, xhr){
           // do something here with response;
         });
    });
});

0
投票

你也可以这样做:

  • 向表单添加 Id
<form method="post" id="myForm">
    <button type="submit">Submit</button>
</form>
  • 在按钮中添加表单引用
<button type="submit" form="myForm" class="btn btn-primary">Save changes</button>
© www.soinside.com 2019 - 2024. All rights reserved.