jQuery的/阿贾克斯的cookie电话 [英] Jquery/Ajax cookie call
问题描述
我怎么会让一个AJAX调用,如果一个页面检测cookie是present?下面的code类火灾警报,但没有Ajax调用。任何帮助非常AP preciated。
编辑:更新及以下完全正常的code。只是不得不改变$(本),以$('更'),该cookie时发现。非常感谢帮助/建议。
$(文件)。就绪(函数(){
//更多按钮
$('更')。住(点击,函数()
{
$ .cookie('viewing_expanded_content',真的,{到期,新的日期(新的日期()的valueOf()+ 60 * 60 * 1000)。});
VAR ID = $(本).attr(ID);
如果(ID)的
{
$(#以上+ ID)的.html('< IMG SRC =图像/ more_ press.gifALT =更多结果.../>');
$阿贾克斯({
键入:POST,
网址:more_ press.php
数据:lastmsg =+ ID,
缓存:真正的,
成功:函数(HTML){
$(#DIV更新)添加(HTML);
$(#以上+ ID)上卸下摆臂();
}
});
} 其他 {
$(morebox)HTML('< P><强>没有更多的结果...< / STRONG>< / P>');
}
返回false;
});
变种viewing_expanded_content = $ .cookie('viewing_expanded_content');
如果(viewing_expanded_content =='真'){
//警报(有效饼干!);
VAR ID = $('更')ATTR(ID)。
如果(ID)的
{
$(#以上+ ID)的.html('< IMG SRC =图像/ more_ press.gifALT =更多结果.../>');
$阿贾克斯({
键入:POST,
网址:more_ press.php
数据:lastmsg =+ ID,
缓存:真正的,
成功:函数(HTML){
$(#DIV更新)添加(HTML);
$(#以上+ ID)上卸下摆臂();
}
});
}
}
})
您确定Ajax调用不被解雇?如果您在成功添加
功能?警报()
会发生什么
更新:我通过您参考code 的工作,但是很难告诉你想做什么。你设置一个cookie,使一个Ajax调用一个更多
元素被点击时。那么你有一些code,检查该Cookie,并使得Ajax调用,如果该cookie设置。
在什么情况下应该在code表示第二位运行?如果它发生在页面加载?如果是的话,它应该得到 ID值
的?
P.S。这:
$(文件)。就绪(函数(){
$(函数(){
就没有意义了。第二行是第一个快捷方式,所以有两个是多余的;刚刚摆脱了第一行(和它匹配的})
)。
How would I make an ajax call if a page detects a cookie is present? The code below fires the alert but not the ajax call. Any help greatly appreciated.
EDIT: Updated and fully working code below. Simply had to change $(this) to $('.more') when the cookie is found. Many thanks for help/advice.
$(document).ready(function(){
//More Button
$('.more').live("click",function()
{
$.cookie('viewing_expanded_content',true, { expires : new Date(new Date().valueOf() + 60 * 60 * 1000) });
var ID = $(this).attr("id");
if(ID)
{
$("#more"+ID).html('<img src="images/more_press.gif" alt="More results..." />');
$.ajax({
type: "POST",
url: "more_press.php",
data: "lastmsg="+ ID,
cache: true,
success: function(html){
$("div#updates").append(html);
$("#more"+ID).remove();
}
});
} else {
$(".morebox").html('<p><strong>No more results...</strong></p>');
}
return false;
});
var viewing_expanded_content = $.cookie('viewing_expanded_content');
if ( viewing_expanded_content == 'true' ) {
//alert("Active cookies!");
var ID = $('.more').attr("id");
if(ID)
{
$("#more"+ID).html('<img src="images/more_press.gif" alt="More results..." />');
$.ajax({
type: "POST",
url: "more_press.php",
data: "lastmsg="+ ID,
cache: true,
success: function(html){
$("div#updates").append(html);
$("#more"+ID).remove();
}
});
}
}
})
Are you sure the ajax call isn't being fired? What happens if you add an alert()
in the success
function?
Update: I'm working through your referenced code, but it's difficult to tell what you're trying to do. You're setting a cookie and making an ajax call when a .more
element is clicked. You then have some code that checks the cookie, and makes the ajax call if that cookie is set.
In what circumstances should that second bit of code run? Should it happen on page load? If so, where should it get the value of ID
from?
P.S. This:
$(document).ready(function(){
$(function() {
doesn't make sense. The second line is a shortcut for the first, so having the two is redundant; just get rid of that first line (and its matching })
).
这篇关于jQuery的/阿贾克斯的cookie电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!