我的网站中有一个列表,当我点击每个列表项时,我希望它们旁边的div用ajax重新加载,以免重新加载整个页面。
这是我的javascript
parameters = "category_id="+categoryId;
var result = ajaxFunction("changeCategory.php", parameters);
$("#mydiv").html(result);
ajaxFunction()函数是常规$ .ajax()jQuery函数,带有“POST”。在“changeCategory.php”中,我调用了另一个php文件。 问题是重新加载整个页面而不是div。我想使用这个ajax函数,因为我想将数据发送到我的php文件。
有谁知道我该怎么办才能重新加载div?
提前致谢
答案 0 :(得分:1)
尝试使用load加载包含网址内容的div -
$("#mydiv").load("changeCategory.php", {category_id: "category_id_value"} );
您可以将数据传递到网址。
如果数据作为对象提供,则使用POST方法;否则,假设GET。
答案 1 :(得分:1)
试试这个
$(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]...');
}
});
});
同时验证在您的点击事件中是否写入此行return false;
这是必需的。
答案 2 :(得分:0)
你可以向该PHP发送一个查询,因此它“理解”它只需要输出div,如下所示:
您的javascript中:
//add the query here
parameters = "category_id="+categoryId + "&type=divonly";
var result = ajaxFunction("changeCategory.php", parameters);
$("#mydiv").html(result);
“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
}
答案 3 :(得分:0)
重新加载整个页面,这意味着您的javascript代码可能存在错误
再次检查 或尝试这个
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;
}
你可以使用get方法或post方法....
答案 4 :(得分:0)
$(document).ready(function() {
$.ajax({
url: "right.php",
type: "POST",
data: {},
cache: false,
success: function (response) {
$('#right_description').html(response);
}
});
});