繁体   English   中英

复选框无法正常工作

[英]Checkbox not working properly

这是我想要的这样的我在jquery实际上中的代码,其中:

  • 默认情况下,“球”的值将显示在文本框中。
  • 同时All或Stopall都可以工作(在这里不能正常工作:()
  • 多次检查“全部”按钮,该按钮不符合预期

这是小提琴链接: http : //jsfiddle.net/bigzer0/PKRVR/11/

$(document).ready(function() {
    $('.check').click(function(){
        $("#policyName").val('Start');
        $("#features").val('');

        $('[name="startall"]').on('click', function() {
        var $checkboxes = $('input[type="checkbox"]').not('[name="startall"], [name="stopall"]');
        if (this.checked) {
            $checkboxes.prop({
                checked: true,
                disabled: true
            });
        }
        else{
             $checkboxes.prop({
                checked: false
            });
        }
    });

                  $(".check").each(function(){
            if($(this).prop('checked')){

                $("#policyName").val($("#policyName").val() + $(this).val());    
                $("#features").val($("#features").val() + $(this).data('name'));
                }            
        });

     });
});

在此情况下,我们欢迎您提出任何意见

您的代码在很多方面都被破坏了。 您正在将click事件绑定到click事件中。 您应该把它放在外面,并确保它在document.ready函数中,因为您的元素是静态元素。

$(document).ready(function() {    
    // cache features
    var $features = $('#features');
    // cache policyname
    var $policy = $("#policyName");
    // cache all/stopall
    var $ss = $('[name="startall"],[name="stopall"]');
    // cache all others
    var $checkboxes = $('input[type="checkbox"]').not($ss);

    // function to update text boxes
    function updateText() {
        var policyName = 'Start';
        var features = '';
        // LOOP THROUGH CHECKED INPUTS - Only if 1 or more of the 3 are checked
        $checkboxes.filter(':checked').each(function(i, v) {
            policyName += $(v).val();
            features += $(v).data('name');
        });
        // update textboxes
        $policy.val(policyName);
        $features.val(features);
    }

    $checkboxes.on('change', function() {
        updateText();
        // check startall if all three boxes are checked
        $('input[name="startall"]').prop('checked', $checkboxes.filter(':checked').length == 3);
    });

    $('input[name="startall"]').on('change', function() {
        $checkboxes.prop({
            'checked': this.checked,
            'disabled': false
        });
        updateText();
    });

    $('input[name="stopall"]').on('change', function() {
        $checkboxes.add('[name="startall"]').prop({
            'checked': false,
            'disabled': this.checked
        });
        updateText();
    });

    // updatetext on page load
    updateText();
});​

小提琴

您正在检查点击功能中的点击功能。 您应该使用if语句。

暂无
暂无

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

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