简体   繁体   English

jQuery函数问题

[英]jQuery problems with function

What i'm trying to do here is if the selection is equal to value 10 the click function to be available only if selection is equal to 10. But when i change to other ex. 我在这里要做的是,如果选择等于值10,则只有当选择等于10时,单击功能才可用。但是当我改为其他前。 category with different value the radio click function is still available. 具有不同值的类别无线电点击功能仍然可用。 ?

I have 6 radio boxes with value 1,2,3,4,5,6 so what i want to do if value == 4 to slidedown another div while i'm in category with value 10.(selection). 我有6个价值1,2,3,4,5,6的收音机盒,所以当我在类别10中时,如果值== 4滑入另一个div,我想做什么。(选择)。

How can i fix this problem ? 我该如何解决这个问题? Here is my sample code. 这是我的示例代码。

$('#category').on('change', function () {

    var selection = $(this).val();
    $('#slidedown'+selection).slideDown(200);

    if(selection == '10'){
        $("input:radio[name='checkbox']").click(function() {
            var radio = $(this).val();
            if(radio == '4' && selection == '10') {
                $('#slidedown'+selection).slideUp();    
            } else {
                $('#slidedown'+selection).slideDown();  
            }                    
        });
    });

Thanks, any help will be appreciated. 谢谢,任何帮助将不胜感激。

EDIT : I want to slideUp the currect div which is slided down by the category value if radio box with value 4 is checked. 编辑:如果选中值为4的单选框,我想滑动当前div,它被类别值向下滑动。

You should have another selection var inside the click callback: 您应该在click回调中有另一个selection var:

$('#category').on('change', function () {
    var selection = $(this).val();
    $('#slidedown'+selection).slideDown(200);            
});    

    $("input:radio[name='checkbox']").click(function() {
        var selection = $('#category').val();   //This does the trick
        var radio = $(this).val();
        if(radio == '4' && selection == '10') {
            $('#slidedown_another').slideUp();    
        } else {
            $('#slidedown_another').slideDown();  
        }                    
    });

Also, callbacks must be separated for not binding a new listener each time 此外,必须将回调分开,以便每次都不绑定新的侦听器

Hope this helps. 希望这可以帮助。 Cheers 干杯

Use the disabled property to enable and disable the radio buttons. 使用disabled属性可启用和禁用单选按钮。

$('#category').change(function() {
    var selection = $(this).val();
    $('#slidedown'+selection).slideDown(200);

    $('input:radio[name=checkbox]').prop('disabled', selection != '10');
});

$("input:radio[name='checkbox']").click(function() {
    var radio = $(this).val();
    if(radio == '4') {
        $('#slidedown_another').slideUp();    
    } else {
        $('#slidedown_another').slideDown();  
    }                    
});

Your code is adding a handler when the select has the correct value, but it never removes the handler when the select changes to a different value. 当select具有正确的值时,您的代码正在添加处理程序,但是当select更改为其他值时,它永远不会删除处理程序。 Also, every time they select 10 it was adding another handler, so the handler would then run multiple times. 此外,每次他们选择10都会添加另一个处理程序,因此处理程序将运行多次。

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

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