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 );
           }
         });
    });
});

我不知道该怎么做,希望得到帮助:-)

4

4 回答 4

1

Try something like this:

   <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 );
           }
         });
    });
});
于 2013-05-28T08:10:39.927 回答
1

您必须获取颜色值并在单击事件上调用 ajax

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 );
    });
    */
    });
});
于 2013-05-28T08:49:05.730 回答
0

为您的列表元素获取相同的类:

<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 );
       }
     });
于 2013-05-28T08:21:32.493 回答
0

See which class is adding when you are changing the theme. It may add to your page's body. If so then store that class into jQuery cookies or you can use php session/cookies [via ajax]. Then add php session/cookie variable to body. Like this <body class="<?php echo $_SESSION['themeName'] ?>">

于 2013-05-28T08:26:56.773 回答