简体   繁体   English

切换功能按预期在jQuery 1.9.1+中运行

[英]toggle function working as expected in jQuery 1.9.1+

I am using jQuery to select a list of checkboxes. 我正在使用jQuery选择复选框列表。 I am using the code as outlined in this fiddle: 我正在使用此小提琴中概述的代码:

$(document).ready(function(){
    $('.check:button').toggle(function(){
        $('input:checkbox').attr('checked','checked');
        $(this).val('uncheck all')
    },function(){
        $('input:checkbox').removeAttr('checked');
        $(this).val('check all');        
    })
})

http://jsfiddle.net/gubhaju/Vj6wY/3/ http://jsfiddle.net/gubhaju/Vj6wY/3/

It was working in the fiddle, but not on my site (the button was disappearing). 它正在摆弄,但在我的网站上却没有(按钮消失了)。 I then saw that the fiddle was using version 1.4.4, whereas my site was using version 2.1.1. 然后,我看到小提琴使用的是1.4.4版,而我的网站使用的是2.1.1版。 After playing with the fiddle, I found that only with jQuery versions 1.8.3 and lower was the select working. 在玩弄小提琴之后,我发现只有使用jQuery 1.8.3及更低版本的jQuery才有效。 What changed between 1.8.3 and 1.9.2? 1.8.3和1.9.2之间有什么变化? How can I modify this code so that it works for my version? 如何修改此代码,使其适用于我的版本?

As I showed you in my comment, there are quite a few changes in the versions of jQuery that you refer to. 正如我在评论中向您展示的那样,您引用的jQuery版本有很多更改。 You can use the jQuery Migrate plugin (made by the jQuery team) to help troubleshoot the changes, but in a nutshell you can boil your code down to: 您可以使用jQuery Migrate插件(由jQuery团队制作)来帮助解决更改问题,但总而言之,您可以将代码简化为:

 $('.check').click(function () { $('input:checkbox').prop('checked', !$('input:checkbox').prop('checked')); $(this).val(($(this).val() == 'uncheck all') ? 'check all' : 'uncheck all') }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="button" class="check" value="check all" /> <input type="checkbox" class="cb-element" />Checkbox 1 <input type="checkbox" class="cb-element" />Checkbox 2 <input type="checkbox" class="cb-element" />Checkbox 3 

Here is a possible work-around for you to use in newer versions (keeping your original code as intact as possible) 这是您可以在较新版本中使用的一种可能的解决方法(保持原始代码尽可能完整)

JSFiddle 的jsfiddle

$(document).ready(function(){
    $('.check:button').click(function(){
        if($(this).val() === 'check all') {
            $('input:checkbox').prop('checked', true);
            $(this).val('uncheck all')
        } else {
            $('input:checkbox').prop('checked', false);
            $(this).val('check all');        
        }
    })
})

Notice you'll also have to use prop() instead of attr() to set checked. 注意,您还必须使用prop()而不是attr()来设置checked。

One more possible workaround: 另一种可能的解决方法:

 $('.check:button').click(function () { var toggled = this.value == 'check all'; $('input:checkbox').prop('checked', toggled); this.value = toggled ? 'uncheck all' : 'check all'; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="button" class="check" value="check all" /> <input type="checkbox" class="cb-element" />Checkbox 1 <input type="checkbox" class="cb-element" />Checkbox 2 <input type="checkbox" class="cb-element" />Checkbox 3 

Your toggle(Function, Function) no longer exists from jQuery 1.9 upwards. 从jQuery 1.9开始您的toggle(Function,Function)不再存在

You can restore functionality by using a simple variable to track the state: 您可以通过使用简单的变量来跟踪状态来恢复功能:

$(document).ready(function(){
var state = 1;
    $('.check:button').click(function(){
        if (state) {
            $('input:checkbox').attr('checked','checked');
            $(this).val('uncheck all')
        } else {
            $('input:checkbox').removeAttr('checked');
            $(this).val('check all');
        }
        state = !state;
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM