我希望表单提交到同一页面,因此我将表单操作保留为黑色。我尝试过使用<button type="submit">
,<input type="button">
和<input type="submit">
。 Here is the live version了解更多背景信息。当您单击“添加学生”时,单击“保存学生信息”按钮,它不执行任何操作,但关闭按钮可以正常工作。
这是我的模态的代码。
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Add a new student</h4>
</div>
<div class="modal-body">
<form class="form-horizontal" role="form" method="post" action="">
<div class="form-group">
<div class="col-sm-offset-1 col-sm-10">
<input type="text" class="form-control" id="inputID" placeholder="Student ID Number">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-10">
<input type="text" class="form-control" id="inputLast" placeholder="Last Name">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-10">
<input type="text" class="form-control" id="inputFirst" placeholder="First Name">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-10">
<input type="text" class="form-control" id="inputAddress" placeholder="Street Address">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-10">
<input type="text" class="form-control" id="inputCity" placeholder="City">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-10">
<input type="text" class="form-control" id="inputState" placeholder="State">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-10">
<input type="text" class="form-control" id="inputZip" placeholder="Zip">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-10">
<input type="text" class="form-control" id="inputBalance" placeholder="Current Balance">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-10">
<input type="text" class="form-control" id="inputTerm" placeholder="First Term Attended">
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<input type="submit" value="Save Student Information" id="submit" name="submit" class="btn btn-primary">
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
答案 0 :(得分:7)
您还可以使用HTML5属性:form
链接表单外部的按钮(或输入) 来自w3schools网站的例子:<form action="demo_form.asp" method="get" id="form1">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
</form>
<button type="submit" form="form1" value="Submit">Submit</button>
答案 1 :(得分:5)
您的提交按钮位于代码之外。只需让表单包裹整个模态窗口就像这样...
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<form class="form-horizontal" role="form" method="post" action="">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Add a new student</h4>
</div>
<div class="modal-body">
<div class="form-group">
<div class="col-sm-offset-1 col-sm-10">
<input type="text" class="form-control" id="inputID" placeholder="Student ID Number">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-10">
<input type="text" class="form-control" id="inputLast" placeholder="Last Name">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-10">
<input type="text" class="form-control" id="inputFirst" placeholder="First Name">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-10">
<input type="text" class="form-control" id="inputAddress" placeholder="Street Address">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-10">
<input type="text" class="form-control" id="inputCity" placeholder="City">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-10">
<input type="text" class="form-control" id="inputState" placeholder="State">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-10">
<input type="text" class="form-control" id="inputZip" placeholder="Zip">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-10">
<input type="text" class="form-control" id="inputBalance" placeholder="Current Balance">
</div>
</div>
<div class="form-group">
<div
class="col-sm-offset-1 col-sm-10">
<input type="text" class="form-control" id="inputTerm" placeholder="First Term Attended">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<input type="submit" value="Save Student Information" id="submit" name="submit" class="btn btn-primary">
</div>
</div><!-- /.modal-content -->
</div>
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
答案 2 :(得分:1)
删除
中的属性data-dismiss = "modal"
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
答案 3 :(得分:0)
因为提交按钮位于表单元素之外。
将表单元素包装在.modal-body周围 - 这将确保所有表单字段和输入按钮都在要提交的表单中。
此外,将一些样式应用于具有0填充和0边距的表单,因为它可能会推动表单使模态看起来不整洁。
希望它有所帮助,这就是我所做的,它对我来说很好。