问题
I have a page that calls a bootstrap modal window with external content. The modal window consists of a form. The form is using jquery validate and I'm trying to use an ajax submit handler. The problem is that it doesn't seem I can access the main div I want to update from. The modal window is using its own html page. I want to update a div that was used to call the modal window, is something like this possible?
No code unless you want me to post to better clarify. Hope what I want to do makes sense.
edit: [Main page is what I want to update from ajax call in modal]
[Main Page]
<div id='div-id-want-to-update-on-submit'></div>
<a href='/some-external-form' data-toggle='modal' data-target='#my-modal'>Launch Modal</a>
[Modal Window]
<div id="my-modal" class="modal hide fade">
<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 action=/post-somewhere>
...
</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>
<script type="text/javascript">
... some jquery to validate and ajax call to another method.
var request;
request = $.ajax({
url: '/post-method/returns-html',
type: "post",
data: serializedData
});
request.done(function (response, textStatus, jqXHR) {
$('#div-id-want-to-update-on-submit').html(response);
});
</script>
[edit2] - Ignore this, I made a stupid type in my update div. no window.opener was needed as recommended below, the DOM is available from the main page.
回答1:
You should be able to reference the opening document with window.opener.document
e.g.
request.done(function (response, textStatus, jqXHR) {
$(window.opener.document).find('#div-id-want-to-update-on-submit').html(response);
});
来源:https://stackoverflow.com/questions/19451972/how-to-update-div-outside-bootstrap-modal