2012-10-10 133 views
1

我不能够实现这些东西,在这个页面中我想做落实3两件事:选择和取消选择所有复选框

1>默认情况下,苹果和猫会在这个页面进行检查。 2>对于所有功能都将被检查和禁用。 3>对于STOPALL,所有的功能也将被默认(AC)值将被禁用,禁用*

<script type="text/javascript"> //This jquery is using for on selecting the checkbox,it will assign the text field of policyName and features 
    $(document).ready(function() { 
     $('.check').click(function(){ 
      $("#policyName").val('Start'); 
      $("#features").val(''); 
        $(".check").each(function(){ 
       if($(this).prop('checked')){ 
        $("#policyName").val($("#policyName").val() + $(this).val()); 
        $("#features").val($("#features").val() + $(this).data('name')); 
        }   
      }); 
     }); 
    }); 
    </script> 

这是我的jsp页面:

<div align="center" id="checkboxes"> 
<input type="checkbox" name="startall" data-name="Startall" class="check" id="check" value="all"> All &nbsp;&nbsp;&nbsp; 
<input type="checkbox" name="stopall" data-name="Stopall" class="check" id="check" value="stopall"> STOPall &nbsp;&nbsp; 
<input type="checkbox" name="apple" data-name="Apple" class="check" id="check" value="a"> Apple &nbsp;&nbsp;&nbsp; 
<input type="checkbox" name="ball" data-name="Ball" disabled="disabled" checked="checked" id="check" value="b"> Ball 
    &nbsp;&nbsp;&nbsp; 
<input type="checkbox" name="cat" data-name="Cat" class="check" id="check" value="a"> Cat &nbsp;&nbsp;&nbsp; 
<input type="checkbox" name="dog" data-name="Dog" checked="checked" disabled="disabled" id="check" value="d"> Dog 
    &nbsp;&nbsp;&nbsp; 
<input type="checkbox" name="elephant" data-name="Elephant" disabled="disabled" checked="checked" id="check" value="e"> 
    Elephant &nbsp;&nbsp;&nbsp; 
</div> 

的任何援助....将被apprciated。

回答

1

在你的HTML试试这个

ID应该独特 ..尝试使用不同的ID或类,而不是..我写使用名称属性,它是一个缓慢的选择的代码..

$(document).ready(function() { 
$('[name="apple"], [name="cat"]').prop('checked', true); 

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

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

});

UPDATED DEMO

+0

感谢您的快速回复;另外一件事是我正在使用文本框,现在的条件是如果选中该框,那么将显示数据名称以及此con文本上的任何输入。 – user1645434

+0

完全没有问题:),文本框会显示在哪里?它会是每个项目的单独文本框 –

+0

有单个文本框,在选择A,B,E时它会显示“StartABE”(这里的Start是选择全部 - >将显示“StartAll”选择停止它将显示“StopAll” – user1645434

相关问题