简体   繁体   English

如何使用Ajax在div中添加页面内容?

[英]How can I add the content of a page in a div with Ajax?

I have a list in my site, and when I click each of the list items, I want the div next to them to reload with ajax, so as not to reload the whole page. 我在我的网站上有一个列表,当我点击每个列表项时,我希望它们旁边的div用ajax重新加载,以免重新加载整个页面。

Here is my javascript 这是我的javascript

  parameters = "category_id="+categoryId;
  var result = ajaxFunction("changeCategory.php", parameters);
  $("#mydiv").html(result);

The ajaxFunction() function is the regular $.ajax() jQuery function, with "POST". ajaxFunction()函数是常规的$ .ajax()jQuery函数,带有“POST”。 In the "changeCategory.php" I call with include another php file. 在“changeCategory.php”中,我调用了另一个php文件。 The problem is that the whole page is reloaded instead of only the div. 问题是重新加载整个页面而不是div。 I want to use this ajax function I have, cause I want to send data to my php file. 我想使用这个ajax函数,因为我想将数据发送到我的php文件。

Does anyone know what should I do to reload only the div? 有谁知道我应该怎么做只重新加载div?

Thanks in advance 提前致谢

Try using load to load the div with the url contents - 尝试使用load来加载带有url内容的div -

$("#mydiv").load("changeCategory.php", {category_id: "category_id_value"} );

You can pass data to the url. 您可以将数据传递到网址。

The POST method is used if data is provided as an object; 如果数据作为对象提供,则使用POST方法; otherwise, GET is assumed. 否则,假设GET。

Try this 尝试这个

$(document).ready(function(){
    var parameters = {category_id:categoryId};
    $.ajax({
        url:'changeCategory.php',
        type:'post',
        data:parameters,
        dataType:'html',
        success:function(result){
            $("#mydiv").html(result);
        },
        error:function(){
            alert('Error in loading [itemid]...');
        }
    });

});

Also verify that when in your click event this line is written or not return false; 同时验证在您的点击事件中写入此行是否return false; This is required. 这是必需的。

you could send a query to that PHP so it "understands" that it needs to output only the div, like this: 您可以向该PHP发送查询,以便它“理解”它只需要输出div,如下所示:

in your javascript: 在你的JavaScript中:

//add the query here
parameters = "category_id="+categoryId + "&type=divonly";
var result = ajaxFunction("changeCategory.php", parameters);
$("#mydiv").html(result);

in your "changeCategory.php": 在你的“changeCategory.php”中:

//add a query check:
$type = "";
if (isset($_POST['type'])) {
    $type = $_POST['type'];
}

//then, depending on the type, output only the div:
if($type === "divonly"){
   //output the div only;
} else {
   //your normal page
}

The whole page is reloaded that means there may be an error in your javascript code 重新加载整个页面,这意味着您的javascript代码中可能存在错误

check it again or try this one 再次检查或尝试这一个

function name_of_your_function(id)
{   

var html =  $.ajax({
   type: "GET",
   url: "ajax_main_sectors.php",
   data: "sec="+id,
   async: false
  }).responseText;


    document.getElementById("your div id").innerHTML=html;
}

you can use get method or post method.... 你可以使用get方法或post方法....

$(document).ready(function() {
    $.ajax({
        url: "right.php",
        type: "POST",
        data: {},
        cache: false,
        success: function (response) {

            $('#right_description').html(response);
        }
    });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM