我有一个表单(#waldform),我只想在提交之前的表单(#modelform form )之后才出现它。#modelform和#waldform都调用python函数。如果我将#waldform直接包含到html文件中,一切都会正常工作,并调用python函数。但是,如果我只在提交#modelform之后,使用jquery (如下面代码中的代码),就不会调用python函数,站点url将在末尾更改为#。
这里是jquery。表单元素被添加到第一个函数中。
$(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 ;
});
});
});如果需要,这里是html。如果我将#waldform直接包含到这个html代码中,那么python函数将被正确调用。
<!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>发布于 2013-10-24 19:16:08
在第二个表单存在之前,您将绑定到第二个表单的提交事件。把它移到你添加它之后。
//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() {发布于 2013-10-24 19:30:06
您需要使用.on()委托它。
$(document).on('submit', '#waldform', function(e) {
alert('clicked');
e.preventDefault();
});https://stackoverflow.com/questions/19574619
复制相似问题