我的表格如下,
<form id="compose" method="post" name="new_message">
<div id="new_message">
<div>
<label class="required" for="new_message_subject">Subject</label>
<input type="text" class="form-control" required="required" name="new_message[subject]" id="new_message_subject">
</div>
<div>
<label class="required" for="new_message_receiver">To</label>
<select class="form-control" name="new_message[receiver]" id="new_message_receiver">
<option value="2">fnamexxxxx lname</option>
<option value="5">tester testerlast</option>
<option value="7">bummer bumlast</option>
</select>
</div>
<div>
<label class="required" for="new_message_content">Content</label>
<textarea class="form-control" required="required" name="new_message[content]" id="new_message_content"></textarea>
</div>
<div>
<button class="btn btn-primary" name="new_message[Compose]" id="new_message_Compose" type="submit">Compose</button>
</div>
<input type="hidden" name="new_message[_token]" id="new_message__token">
</div>
并使用preventDefault()
我已停止表单提交,然后使用bootstrap模式来获取确认消息,您可以在下面看到我的Jquery函数和bootstrap模式。
<script>
document.getElementById("compose").addEventListener("submit", function(event){
event.preventDefault();
var title = $("#new_message_subject").val();
var to = $("#new_message_receiver :selected").text();
var message = $("#new_message_content").val();
$('#title').text(title);
$('#to').text(to);
$('#message').text(message);
$('#myModal').modal('show');
$('#send').click(function(){
// avoid preventDefault() and submit form code here
});
});
</script>
<div class="modal fade" tabindex="-1" role="dialog" id="myModal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Message send confirmation</h4>
</div>
<div class="modal-body">
<h4 id="title"></h4>
<p><b>To</b></p>
<p id="to"></p>
<p><b>Message</b></p>
<p id="message"></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" id="send" class="btn btn-primary">Send</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
场景是当用户点击表单提交按钮,然后弹出引导模式并要求确认表单提交,如果用户单击发送按钮,id="send"
则表单需要执行其操作/提交。
现在我要做的是当用户点击需要提交表单的模式中的发送按钮时。我尝试了几种方法,但无法避免preventDefault()
并提交表单。
答案 0 :(得分:1)
您的脚本代码应如下所示。
<script>
$("#compose").submit(function (objEvent) {
objEvent.preventDefault();
var title = $("#new_message_subject").val();
var to = $("#new_message_receiver :selected").text();
var message = $("#new_message_content").val();
$('#title').text(title);
$('#to').text(to);
$('#message').text(message);
$('#myModal').modal('show');
$(this)[0].submit();
})
</script>