2013-08-13 4 views
0

oldRefreshnewRefresh의 두 변수를 비교하고 싶습니다. 입력에서 oldRefresh의 값 그렇게 쉽게 var oldRefresh= $('#oldrefresh').val();.load()로 변수에 값을 저장하는 방법;

하지만 newRefresh을 입력하여 oldRefresh에 저장, 그것을 얻기 어렵다, 나는이 코드입니다 .load();

와 다른 파일에서 그것을 얻을해야합니다 결과

var newRefresh = setInterval(function() 
{ 
    $('#noti_number').load('include/js_notification_count.php?n=".$_SESSION['username']."'); 
    }); 
}, 5000); 
alert(newRefresh); 

:

var oldRefresh= $('#oldrefresh').val(); 

setInterval(function() 
{ 
    $('#noti_number').load('include/js_notification_count.php?n=".$_SESSION['username']."'); 
    }); 
}, 5000); 

나는이 시도 이 2 때 부하의 결과는 0이어야합니다.

그래서이이

setInterval(function() 
{ 
    var newRefresh = $('#noti_number').load('include/js_notification_count.php?n=".$_SESSION['username']."'); 
    }); 
    alert(newRefresh); 
}, 5000); 

결과가 [object Object]입니다했습니다. 나는 그것을 얻지 않는다. 변수에 load 값을 얻으려면 어떻게해야합니까?

+0

'.load()'를 사용하지 마십시오. DOM 요소의 내용을 Ajax가로드 한 파일로 대체하는 것을 의미합니다. 대신 ['.ajax()'] (http://api.jquery.com/jquery.ajax)를 사용하십시오. – JJJ

+0

또한'load()'는 내가 의도 한 것이라고 생각하지 않는 jQuery 객체를 반환한다. –

+1

당신의 게시 된 질문과 다소 관련이없는 단지 주석 : 당신은'n = ". $ _ SESSION [ 'username']."''js_notification_count.php' 스크립트로 돌아가서'$ _SESSION [ '사용자 이름]'PHP 쪽. 이는 클라이언트 측 및 서버 측 로직을 조금 더 깨끗하게 만들 수 있습니다. – Divey

답변

1

jQuery로드가 개체를 js_notification_count.php 파일에서 반환 된 정보로 바꿉니다. 같은 (반환 된 응답을 가지고 noti_number 필요가없는 경우) 내가 아약스 대신하지만 사용하는 것이

setInterval(function() { 
    $('#noti_number').load('include/js_notification_count.php?n=<?=$_SESSION['username']?>', function(response, status, xhr) { 
     newRefresh = response; 
     alert(newRefresh); 
     } 
    }); 
}, 5000); 

: 당신이는 .text를 (추가) 또는 같은로드 기능을 변경할 수 있습니다

setInterval(function() { 
    $.ajax({ 
     type: "GET", //Change to whatever method type you are using on your page 
     url: "include/js_notification_count.php", 
     data: { n: "<?=$_SESSION['username']?>" } 
    }).done(function(result) { 
     newRefresh = result; 
     alert(newRefresh); 
    }); 
}, 5000); 
+0

은 .text()에 대한 예제를 제공합니다. –

+0

다른 두 가지 예 중 하나를 사용해보십시오. .text()는 .load (url) .text() 바로 뒤에 있지만 신뢰할 수 있는지 확실하지 않습니다. 다른 함수는 호출이 완료된 후에 호출됩니다. – fanfavorite

+0

요청이 작동하지 않았습니다. 아약스에서 세션 이름을 'n :'AbdullahSalma '로 변경 했으므로 결과는 '60'이어야합니다. include/js_notification_count.php'에'if (empty ($ _ G [ 'n'])) {empty};} else {..}'경고는 나를 비운다. –

-1

경우 이렇게하면 값을 비교할 수 있습니다.

$('#noti_number').load(
    'include/js_notification_count.php?n=".$_SESSION['username']."', 
    function(aData) { 
     //Do your comparison here. 
    } 
) 

전달 된 데이터는 서버의 응답이어야합니다.

+0

'oldRefresh'와 무엇을 비교합니까? aData? –

+0

예, 맞습니다. aData는 텍스트 문자열로 서버에서 보낸 정확한 응답입니다. –

관련 문제