0

我有大约 20 个开/关开关,它们发送一个更新表中字段的 ajax 请求。我已经复制了类附加到选择器的位置,因为我需要能够指定哪些开关是打开或关闭的。但是,我单击的第一个按钮可以工作,但在那之后,有时我需要单击一次其他按钮,而其余按钮则需要单击两次。这是一些代码!

附加课程:

$(document).ready(function() {
    var user = $(".profile").attr('id');
    $('#status').hide();
    $('.switch1').checkbox("on", 
        function(theId) {
            $.post("setPerms.php", { user_id: user, val: "1", field: theId }, function(data){
                $("#status").text(data).fadeIn(1000);
                $('#status').fadeOut(1000);
            });
         },
        function(theId) {
            $.post("setPerms.php", { user_id: user, val: "0", field: theId }, function(data){
                $("#status").text(data).fadeIn(1000);
                $('#status').fadeOut(1000);
            });
        });

    $('.switch0').checkbox("off", 
        function(theId) {
            $.post("setPerms.php", { user_id: user, val: "1", field: theId }, function(data){
                $("#status").text(data).fadeIn(1000);
                $('#status').fadeOut(1000);
            });
          },
        function(theId) {
            $.post("setPerms.php", { user_id: user, val: "0", field: theId }, function(data){
                $("#status").text(data).fadeIn(1000);
                $('#status').fadeOut(1000);
            });
        });
});

这里是点击功能的地方:(单独的文件)

// click handling
        jQuery(this).click(function() {
            var theId = $(this).attr('id');
            if(state == 'on') {
                jQuery(this).find('.iphone_switch').animate({backgroundPosition: -53}, "normal", function() {
                    jQuery(this).attr('src', settings.switch_off_container_path);
                    switched_off_callback(theId);
                });
                state = 'off';
            }
            else {
                jQuery(this).find('.iphone_switch').animate({backgroundPosition: 0}, "normal", function() {
                    switched_on_callback(theId);
                });
                jQuery(this).find('.iphone_switch').attr('src', settings.switch_on_container_path);
                state = 'on';
            }
        }); 

如果您需要更多信息,请与我们联系。任何帮助是极大的赞赏!!

4

1 回答 1

0

看起来您有一个全局状态变量,而不是每个开关特定的状态变量。您应该为每个名为 state 的开关添加一个属性并检查它。

于 2009-10-22T17:17:03.987 回答