Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 两个DOM类运行相同的javascript类,破坏了单击功能?_Jquery_Binding_Double Click - Fatal编程技术网

Jquery 两个DOM类运行相同的javascript类,破坏了单击功能?

Jquery 两个DOM类运行相同的javascript类,破坏了单击功能?,jquery,binding,double-click,Jquery,Binding,Double Click,我有大约20个开/关开关,它们发送一个ajax请求,更新表中的一个字段。我复制了类连接到选择器的位置,因为我需要能够指定哪些开关处于打开或关闭状态。但是,我单击的第一个按钮可以工作,但是在那之后,我有时需要单击一次其他一些按钮,而其余的按钮需要单击两次。这里有一些代码 附加类别: $(document).ready(function() { var user = $(".profile").attr('id'); $('#status').hide(); $('.swit

我有大约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);
            });
        });
});
这里是点击功能的位置:(分开文件)


如果你需要更多信息,请告诉我。非常感谢您的帮助

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

我想就是这样,但是如何添加attr?好的,知道了。只是使用data()函数来存储状态变量。再次感谢。
// 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';
            }
        });