2013-05-28 224 views
1

保存主題的變化我有一個網站,我可以通過點擊這樣的顏色更改主題:阿賈克斯

<ul> 
    <li> 
     <span class='red'></span> 
     <span class='orange'></span> 
     <span class='green'></span> 
    </li> 
</ul> 

這部分工作得很好,但是......當清爽的頁面,又回到正常,所以。

我想使用jQuery Ajax將顏色保存在我的數據庫中,但不知道如何將顏色onclick發送到我的jQuery函數?

$(function() { 
    $('.themechange').click(function() { 
     $.ajax({ 
      type: "POST", 
      url: "http://mydomain.com/updatetheme.php", 
      data: "color=somecolor", 
      success: function(msg){ 
      alert("Data Saved: " + msg); 
      } 
     }); 
    }); 
}); 

我不知道如何做到這一點,並希望尋求幫助:-)

+1

,你能告訴我們你的'.themechange' HTML是..what? – bipen

+0

。Themechange沒什麼。只是我放了一些東西,我不知道該把什麼放在這裏? – Mansa

+0

將類'themechange'添加到您的span標記中,比如'class ='red themechange'',並且爲它們中的每一個都添加一個'id',它們的名稱如'id =「red」',然後在'data'屬性中jquery ajax寫入'data:「color =」+ $(this).attr('id'),'。你準備好了! – MahanGM

回答

1

嘗試是這樣的:

<ul> 
     <li> 
      <span class='red themechange' data-color='red'></span> 
      <span class='orange themechange' data-color='orange'></span> 
      <span class='green themechange' data-color='green '></span> 
     </li> 
    </ul> 

的Jquery:

$(function() { 
    $('.themechange').click(function() { 
     $.ajax({ 
      type: "POST", 
      url: "http://mydomain.com/updatetheme.php", 
      data: "color=" + $(this).data('color'), 
      success: function(msg){ 
      alert("Data Saved: " + msg); 
      } 
     }); 
    }); 
}); 
0

GET您的列表元素的同一類:

<ul> 
    <li> 
     <span class='themechange' data-color='red'></span> 
     <span class='themechange' data-color='orange'></span> 
     <span class='themechange' data-color='green '></span> 
    </li> 
</ul> 

(function() { 
$('.themechange').click(function() { 
    $.ajax({ 
     type: "POST", 
     url: "http://mydomain.com/updatetheme.php", 
     data: "color=" + $(this).attr('data-color'), 
     success: function(msg){ 
     alert("Data Saved: " + msg); 
     } 
    }); 
0

查看在更改主題時要添加哪個課程。它可能會添加到您的頁面的正文。如果是這樣,然後將該類存儲到jQuery Cookie中,或者可以使用php會話/ cookie [通過ajax]。然後將php會話/ cookie變量添加到正文。像這樣<body class="<?php echo $_SESSION['themeName'] ?>">

1

你必須得到顏色值,並呼籲click事件阿賈克斯

http://jsfiddle.net/ZmbbA/2/

$(document).ready(function(){ 
    $("li").click(function(){ 

     alert('Here ajax happens with color: ' + $(this).attr("class")); 
    /* 
    $.ajax({ 
     type: "POST", 
     url: "http://mydomain.com/updatetheme.php", 
     data: { color: $(this).attr("class") } 
     }).done(function(msg) { 
     alert("Data Saved: " + msg); 
    }); 
    */ 
    }); 
});