Php AJAX加载消息
我想在通过AJAX检索结果时显示加载消息,但我不能。有人能帮忙吗Php AJAX加载消息,php,jquery,ajax,Php,Jquery,Ajax,我想在通过AJAX检索结果时显示加载消息,但我不能。有人能帮忙吗 <script type="text/javascript"> $(function() { $(".search_button").click(function() { // getting the value that user typed var searchString = $("#search_box").val(); // forming t
<script type="text/javascript">
$(function() {
$(".search_button").click(function() {
// getting the value that user typed
var searchString = $("#search_box").val();
// forming the queryString
var data = 'search='+ searchString;
// if searchString is not empty
if(searchString) {
// ajax call
$.ajax({
type: "POST",
url: "do_search.php",
data: data,
beforeSend: function(html) { // this happens before actual call
$("#results").html('');
$("#search_result_box").show();
$("#searchresults").show();
$(".word").html(searchString);
},
success: function(html){ // this happens after we get results
$("#results").show();
$("#results").append(html);
}
});
}
return false;
});
});
</script>
$(函数(){
$(“.search_按钮”)。单击(函数(){
//获取用户键入的值
var searchString=$(“#搜索框”).val();
//形成查询串
变量数据='搜索='+搜索字符串;
//如果searchString不是空的
如果(搜索字符串){
//ajax调用
$.ajax({
类型:“POST”,
url:“do_search.php”,
数据:数据,
beforeSend:function(html){//这发生在实际调用之前
$(“#结果”).html(“”);
$(“#搜索结果框”).show();
$(“#搜索结果”).show();
$(“.word”).html(搜索字符串);
},
success:function(html){//这在我们得到结果之后发生
$(“#结果”).show();
$(“#结果”).append(html);
}
});
}
返回false;
});
});
我会在触发AJAX请求之前更改消息。因此,单击或提交时:
<script>
$('form').on('submit', function(e) {
e.preventDefault();
$('#response').html('<p>Loading…</p>');
$.post($(this).attr('action'), $(this).serialize(), function(response) {
// do something here with the response
$('#response').html('<p>Request successful.</p>');
});
});
</script>
$('form')。关于('submit',函数(e){
e、 预防默认值();
$('#response').html('load&hellip;');
$.post($(this.attr('action'),$(this.serialize()),函数(response){
//在这里做些什么来回应
$(“#响应”).html(“请求成功。”);
});
});
我会在触发AJAX请求之前更改消息。因此,单击或提交时:
<script>
$('form').on('submit', function(e) {
e.preventDefault();
$('#response').html('<p>Loading…</p>');
$.post($(this).attr('action'), $(this).serialize(), function(response) {
// do something here with the response
$('#response').html('<p>Request successful.</p>');
});
});
</script>
$('form')。关于('submit',函数(e){
e、 预防默认值();
$('#response').html('load&hellip;');
$.post($(this.attr('action'),$(this.serialize()),函数(response){
//在这里做些什么来回应
$(“#响应”).html(“请求成功。”);
});
});
为什么不使用beforeSend和success方法来显示/隐藏加载消息
beforeSend: function(html) { // this happens before actual call
// DO SOMEHTING HERE TO SHOW YOUR LOADING MESSAGE AS $('#loading').show();
$("#results").html('');
$("#search_result_box").show();
$("#searchresults").show();
$(".word").html(searchString);
},
success: function(html){ // this happens after we get results
// DO SOMEHTING HERE TO HIDE YOUR LOADING MESSAGE AS $('#loading').hide();
$("#results").show();
$("#results").append(html);
}
rgds为什么不使用beforeSend和success方法来显示/隐藏加载消息
beforeSend: function(html) { // this happens before actual call
// DO SOMEHTING HERE TO SHOW YOUR LOADING MESSAGE AS $('#loading').show();
$("#results").html('');
$("#search_result_box").show();
$("#searchresults").show();
$(".word").html(searchString);
},
success: function(html){ // this happens after we get results
// DO SOMEHTING HERE TO HIDE YOUR LOADING MESSAGE AS $('#loading').hide();
$("#results").show();
$("#results").append(html);
}
rgds@MartinBean祝你下次好运:)@MartinBean祝你下次好运:)