2012-07-05 48 views

回答

2

這裏有一些代碼使用jQuery發佈到頁面並處理json響應。您將不得不創建一個PHP頁面,該頁面將收到發佈請求並返回您想要的任何內容。

$(document).ready(function() { 

    $.post("/yourpath/page.php", { read: "value1", user: $userNumber}, function (data) { 
     if (data.success) { 
      //do something with the returned json 
     } else { 
      //do something if return is not successful 
     } //if    
    }, "json"); //post 
}); 
0

用更新代碼創建一個新的php文件,然後只是返回一個json,如果它的工作與否。您可以使用$ .getJSON jQuery函數進行設置。

1

創建一個PHP/JSP/.net頁面有兩個參數

mywebsite.com/ajax.php?user=XXX&secondParam=ZZZZ 

武官onClick事件基於它的ID給DIV

$.get("ajax.php?user=XXX&secondParam=ZZZZ". function(data){ 
// here you can process your response and change DIV color if the request succeed 
}); 
0

要選擇的DOM元素在jQuery中,只需執行以下操作:

$("#TheIdOfYourElement") 

你的情況

$("#messageMenuUnread") 

現在,聽的被點擊時它,

$("#messageMenuUnread").click(function(){ 
    //DO SOMETHING 
} 

現在,對於AJAX的樂趣。您可以閱讀http://api.jquery.com/category/ajax/更多技術細節的文件,但是這是它歸結爲

 $("#TheIdOfYourImage").click(function(){ 
      $.ajax({ 
       type: "POST",         // If you want to send information to the PHP file your calling, do you want it to be POST or GET. Just get rid of this if your not sending data to the file 
       url: "some.php",        // The location of the PHP file your calling 
       data: "name=John&location=Boston",   // The information your passing in the variable1=value1&variable2=value2 pattern 
       success: function(result){ alert(result) } // When you get the information, what to do with it. In this case, an alert 
      }); 
     } 

至於顏色變化,可以使用.css()方法

$("#TheIdOfAnotherElement").css("background-color","red") 
0

使用更改CSS jQuery.ajax()

你的代碼看起來像

<!DOCTYPE html> 
<head> 

</head> 
<body> 
    <!-- your button --> 
    <div id="messageMenuUnread"></div> 
    <!-- place to display result --> 
    <div id="frame1" style="display:block;"></div> 

    <!-- load jquery --> 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
    <script> 
     $(document).ready(function(){ 
      //attach a function to messageMenuUnread div 
      $('#messageMenuUnread').click (messageMenuUnread); 
      //the messageMenuUnread function 
      function messageMenuUnread() { 
       $.ajax({ 
        type: "POST", 
        //change the URL to what you need 
        url: "some.php", 
        data: { read: "0", user: "$userNumber" } 
       }).done(function(msg) { 
        //output the response to frame1 
        $("#frame1").html("Done!<br/>" + msg); 
       }); 
      } 
     } 
    </script> 
</body>