如上所述,preventDefault
它不起作用,return false
或者stopImmediatePropagation
,尝试了我能找到/想到的一切,但它不起作用。在我输入所有必填字段并单击按钮后,我的页面将我重定向回我的第一个选择。它应该将我重定向到 textarea 而不是第一个选择
html->
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Sig Generator</title>
<script type="text/javascript" src ="jquery-2.0.3.min.js"></script>
<script type="text/javascript" src="sig.js"></script>
</head>
<body>
<div id="legalform" align ="center">
Rechtsform auswählen:<br><br>
<select id="selection" size="1">
</select><br><br>
<input id="sendLegalForm" type="button" value ="Weiter"/>
</div>
<div id="fields" align="center">
<form id="fieldsForm" >
<br>
<br>
<input id="sendFields" type="submit" value="Weiter"/>
</form>
<br>
</div>
<div id="display" >
<textarea id="textarea" cols="30" rows="20"></textarea>
</div>
</body>
</html>
和 jquery ->
$(document).ready(function() {
var $selection ="";
$.ajax({
type:'GET',
url:'http://localhost/php/sig.php?legalforms=true',
dataType:'json',
success: function (data){
$("#display").hide();
var selection = [];
for(var i = 0; i<data.length; i++){
selection.push('<option>', data[i],'</option>');
}
$("#selection").html(selection.join(''));
$("#fields").hide();
},
error: function(jqXHR,textStatus,errorThrown){
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
}
});
$("#sendLegalForm").click(function () {
selection = $('#selection').val();
$.ajax({
type:'GET',
url:'http://localhost/php/sig.php?selectedLegalform='+ selection,
dataType:'json',
success: function (data){
$("#legalform").hide();
$("#fields").show();
var fieldnames =[];
for(property in data.namesToSubmit){
fieldnames.push(property);
}
var fields=[];
for(var i=0; i<data.textfieldHeaders.length; i++){
fields.push(data.textfieldHeaders[i],'<br>','<input name="',fieldnames[i],'" type="text" ',data.namesToSubmit[fieldnames[i]] == "required"?"required":"",'>','<br/>');
}
fields.push("<br>", 'Pflichtfelder (*)');
$("#fieldsForm").prepend(fields.join(''));
},
error: function(jqXHR,textStatus,errorThrown){
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
}
});
});
//i have tried with the id of the form and button
$("#fieldsForm").on('submit',function(e){
e.preventDefault();
// e.stopImmediatePropagation();
$.ajax({
type:'POST',
url: 'http://localhost/php/sig.php?fieldsend='+selection,
data: $("#fieldsForm").serialize(),
success: function (data){
$('#fields').hide();
$('#display').show();
$("#textarea").html(data);
},
error: function(jqXHR,textStatus,errorThrown){
alert('Bitte alle Pflichtfelder ausfüllen.');
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
}
});
});
});