Ajax显示结果而不刷新页面
当点击submit按钮,结果显示在div中id为“storelisting”的页面上,而不刷新页面时,我尝试访问以下表单 问题是当我点击提交按钮时,它仍然只是重新加载页面?当页面刷新时,它甚至不会在storelisting id中显示任何结果Ajax显示结果而不刷新页面,ajax,Ajax,当点击submit按钮,结果显示在div中id为“storelisting”的页面上,而不刷新页面时,我尝试访问以下表单 问题是当我点击提交按钮时,它仍然只是重新加载页面?当页面刷新时,它甚至不会在storelisting id中显示任何结果 <form id="mullerlocator" method="POST"> <label>Plz: </label> <input type="text" name="p
<form id="mullerlocator" method="POST">
<label>Plz: </label>
<input type="text" name="postcode" maxlength="6" style="display:inline!important; width:100px!important"/> <label>Radius: </label>
<select name="radius""><option value='5'>5 km</option><option value='10'>10 km</option><option value='15'>15 km</option><option value='20'>20 km</option></select><br />
<input type="submit" name="locationsearch" id="mullersubmit" value="Search" style="display:inline!important" />
</form>
<br />
<div id="storelisting"></div>
<script>
$( "#mullersubmit" ).submit(function( event )
{
$.ajax({
type: "POST",
url: "http://www.kissnypro.de/test2/test.php",
data: $( "#mullerlocator" ).serialize(),
success: function(data){
//print here
$("#storelisting").html(data);
},
dataType: 'html' // for json response or 'html' for html response
});
}
</script>
请:
半径:
默认情况下,表单提交将导致页面刷新并发回服务器以供进一步执行。由于您使用JavaScript将表单内容发送回服务器进行处理,因此在JavaScript函数中添加以下行:
$( "#mullersubmit" ).submit(function( event ) {
event.preventDefault(); // <---- Add this line
$.ajax({
type: "POST",
url: "http://www.kissnypro.de/test2/test.php",
data: $( "#mullerlocator" ).serialize(),
success: function(data) {
// print here
$("#storelisting").html(data);
},
dataType: 'html' // for json response or 'html' for html response
});
});
$(“#mullersubmit”).submit(函数(事件){
event.preventDefault();//默认情况下,表单提交将导致页面刷新并发回服务器以供进一步执行。由于您正在使用JavaScript将表单内容发送回服务器进行处理,请在JavaScript函数中添加以下行:
$( "#mullersubmit" ).submit(function( event ) {
event.preventDefault(); // <---- Add this line
$.ajax({
type: "POST",
url: "http://www.kissnypro.de/test2/test.php",
data: $( "#mullerlocator" ).serialize(),
success: function(data) {
// print here
$("#storelisting").html(data);
},
dataType: 'html' // for json response or 'html' for html response
});
});
$(“#mullersubmit”).submit(函数(事件){
event.preventDefault();//这将阻止实际提交
$(function() {
$('form#mullerlocator').on('submit', function(e) {
$.post('test.php', $(this).serialize(), function (data) {
$("#storelisting").html(data);
}).error(function() {
// This is executed when the call to test.php failed.
});
e.preventDefault();
});
});
这将阻止实际提交
$(function() {
$('form#mullerlocator').on('submit', function(e) {
$.post('test.php', $(this).serialize(), function (data) {
$("#storelisting").html(data);
}).error(function() {
// This is executed when the call to test.php failed.
});
e.preventDefault();
});
});