【发布时间】:2014-11-27 08:19:16
【问题描述】:
我已经关注了 Bootstrap Modal 对话框的 HTML 代码:
<div class="modal fade" id="rebateModal" 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">Submit Form</h4>
</div>
<div class="modal-body">
<p style="text-align: justify;"><span style="font-weight: 700;"></p>
<br/>
<!-- Here I want to dynamically add the HTML from AJAX response -->
<form id="request_form" method="post" class="form-horizontal" action="">
</form>
</div>
</div>
</div>
</div>
在上面的代码中我想在
之后动态添加HTML代码<div class="modal-body">
<p style="text-align: justify;"><span style="font-weight: 700;"></p>
<br/>
使用 jQuery AJAX。
为此,我尝试了以下代码:
$('#request_form').submit(function(e) {
var form = $(this);
var formdata = false;
if(window.FormData) {
formdata = new FormData(form[0]);
}
var formAction = form.attr('action');
$.ajax({
url : 'xyz.php',
type : 'POST',
cache : false,
data : formdata ? formdata : form.serialize(),
contentType : false,
processData : false,
success: function(response) {alert(response);
// Below variable contains the HTML code that I want to add after <br/>
var htmlString = "<div class='alert alert-danger alert-dismissible' role='alert'><button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>"+response+"</div>"
$(htmlString).insertBefore('div.modal-body:first-child');
}
});
e.preventDefault();
});
但我做不到。在控制台中出现错误
ReferenceError: htmlString is not defined
Bootstrap 模式中没有新的 HTML。
变量response 包含以下字符串:
Id can't be blank<br>Please select Date<br>Image can't be blank<br>
请在这方面帮助我。
【问题讨论】:
-
您可以将
formdata ? formdata : form.serialize()缩短为formdata || form.serialize()。
标签: javascript jquery html ajax twitter-bootstrap