我在模态中确实有形式。我试图实现的是重写默认的POST方法并使用AJAX。我这样做是为了防止页面切换。然而,似乎脚本并没有被触发。如果我删除脚本,就会发生正常的POST。代码如下:
<div class="modal" id="modal1" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action="process.php" method="POST" id="submitform">
<div class="modal-body">
<div class="form-group">
<label for="source">source:</label>
<textarea class="form-control" id="kaynak" name="kaynak" rows="6"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Ä°ptal</button>
<button type="submit" class="btn btn-primary ">Kaydet</button>
</div>
</form>
</div>
</div>
$(document).ready(function() {
$('#submitform').submit(function(event) {
event.preventDefault();
var post_url = $(this).attr("action");
var request_method = $(this).attr("method");
var formData = $(this).serialize();
var username = "<?php echo $username ?>";
formData.push({
name: 'username',
value: username
});
$.ajax({
type: request_method,
url: post_url,
data: formData,
dataType: 'json',
encode: true
})
.done(function(data) {
console.log(data);
});
})