Reputation: 25349
I've got a form (#waldform) that I want to appear only after a previous form (#modelform) is submitted. Both #modelform and #waldform call python functions. If I include #waldform directly into the html file, everything works fine and the python function is called. However, if I make it appear only after #modelform is submitted, using jquery, like in my code below, the python function isn't called and the site url changes to # at the end.
Here is the jquery. The form element is added in the first function.
$(document).ready(function() {
//call estimate python function
$(function() {
$("#modelform").submit(function() {
// post the form values via AJAX...
$.post('/estimate', {name: $("#mymodel").val()}, function(data) {
var $a_var = data['title']
var $element = $('<div class="item">' + $a_var + '</div><br>');
$('body').append($element) ;
//FORM ADDED HERE
$('body').append('<form id="waldform" action="#" method="post"><input type="text" id="waldnum" /><input type="submit" value="Wald Test" /></form>');
});
return false ;
});
});
//call wald python function
$(function() {
$("#waldform").submit(function() {
//post the form values via AJAX...
$.post('/wald', {name: $("#waldnum").val()}, function(data) {
var $a_var = data['title']
var $element = $('<div class="item">' + $a_var + '</div><br>');
$('body').append($element) ;
});
return false ;
});
});
});
Here is the html if needed. If I include #waldform directly into this html code then the python function is called correctly.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="static/jquery-1.4.2.js"></script>
<script type="text/javascript" src="static/jquery-ui-1.8.4.custom.min.js"></script>
<script type='text/javascript' src='static/js_test.js'></script>
<script type='text/javascript' src='static/jquery.form.js'></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
</head>
<body>
<form id="dataform" action="submit" method="post" enctype="multipart/form-data">
<input type="file" name="myfile" id="myFile"/>
<input type="submit" value="Submit File"/>
</form>
<form id="modelform" action="#" method="post">
<input type="text" id="mymodel" />
<input type="submit" value="Estimate" />
</form>
</body>
</html>
Upvotes: 0
Views: 140
Reputation: 3281
You need to delegate it using .on()
$(document).on('submit', '#waldform', function(e) {
alert('clicked');
e.preventDefault();
});
Upvotes: 0
Reputation: 95017
You're binding to the submit event of the 2nd form before the 2nd form exists. Move it to after you add it.
//FORM ADDED HERE
$('body').append('<form id="waldform" action="#" method="post"><input type="text" id="waldnum" /><input type="submit" value="Wald Test" /></form>');
//BIND EVENT HERE
$("#waldform").submit(function() {
Upvotes: 1