我知道这个问题已经问过无数次了,我看过几个例子,但我还是不知道为什么要提交这个表格。Ajax似乎没有被调用,所以我认为它就是简单的divid问题。这30分钟我一直觉得很沮丧......
JS:
$('#genform').submit(function (e) {
alert('hi');
e.preventDefault();
$.ajax({
url: "month.php",
type: "post",
data: $('#form').serialize(),
success: function (msg) {
$("#info").html(msg);
}
});
});
HTML:
<!-- trigger button -->
<div class="col-md-4">
<a href="#" id="toggle" class="btn btn-default dropdown-toggle btn-sm"> Bulk PDF Export <span class="caret"></span></a>
</div>
<!--- popup form div -->
<div id="gendiv" style="display:none;">
<form id="genform">
<div class="form-input">
<select name="month">
<option value="2013-09-01">September 2013</option>
<option value="2013-08-01">August 2013</option>
<option value="2013-07-01">July 2013</option>
</select>
</div>
<div class="form-input"><i class="icon-ellipsis-horizontal"></i> PGY-1 <span class="pull-right"><input type="checkbox" id="pgy1" checked name="pgy[1]"></span>
</div>
<div class="form-input"><i class="icon-ellipsis-horizontal"></i> PGY-2 <span class="pull-right"><input type="checkbox" id="pgy2" checked name="pgy[2]"></span>
</div>
<div class="form-input"><i class="icon-ellipsis-horizontal"></i> PGY-3 <span class="pull-right"><input type="checkbox" id="pgy3" checked name="pgy[3]"></span>
</div>
<div class="form-input" style="text-align:center">
<button type="submit" class="btn btn-primary btn-xs">Generate</button>
</div>
<div id="info"></div>
</form>
</div>
Fiddle: http://jsfiddle.net/KQ2nM/2/
You are missing some closing tags:
<div class="form-input">
<i class="icon-ellipsis-horizontal"></i> PGY-1 <span class="pull-right">
<input type="checkbox" id="pgy1" checked name="pgy[1]"> </input> <--- here
</span>
</div>
And the form method (otherwise it spits up an error):
<form id="genform" method="POST">
Now django complains about the CSRF token, but that's your stuff ;)
Here's the new Fiddle.
EDIT: It seems like I got it wrong since now it submits without calling your custom handler and Joe fixed it. But you still need to close those inputs :)
The reason it's not working is because the popover clones the form and then places the html inside a div with the class .popover-content
.
This means that the event you bound is only attached to the original #genform
which is inside the hidden #gendiv
.
Use this instead:
$(document).on('submit', '#genform', function(e) {
e.preventDefault();
$.ajax({
url: "month.php",
type: "post",
data: $(this).serialize(),
success: function (msg) {
$("#info").html(msg);
}
});
});
This uses jQuery's .on()
function and attaches an event handler to the document
which basically watches for a submit
event triggered on a form with the id #genform
. By attaching the event handler to the document
instead of directly to the target element it gets triggered by a submit
event regardless of whether a form with the id #genform
exists when the event is bound.
Here it is working: http://jsfiddle.net/KQ2nM/4/