admin管理员组

文章数量:1314587

How to limit checkbox selection to one using jquery or javascript and other checkbox should be disabled after one checkbox selected?

<input type="checkbox" name="size[]" id="size" value="Small" />Small
<input type="checkbox" name="size[]" id="size" value="Medium" />Medium
<input type="checkbox" name="size[]" id="size" value="Large" />Large
<input type="checkbox" name="size[]" id="size" value="Xl" />XL

Here The Example But I Want Same Thing In Html Or Php

/

Problem Is Solved Now Here The Final Solution

<script src=".9.1/jquery.min.js">
</script>

<script>
$(document).ready(function () {
$('input:checkbox').click(function(){
var $inputs = $('input:checkbox')
if($(this).is(':checked')){
$inputs.not(this).prop('disabled',true); 
}else{
$inputs.prop('disabled',false);
}
})
})
</script>

<input type="checkbox">
<input type="checkbox"> 
<input type="checkbox">
<input type="checkbox">
<input type="checkbox" />

How to limit checkbox selection to one using jquery or javascript and other checkbox should be disabled after one checkbox selected?

<input type="checkbox" name="size[]" id="size" value="Small" />Small
<input type="checkbox" name="size[]" id="size" value="Medium" />Medium
<input type="checkbox" name="size[]" id="size" value="Large" />Large
<input type="checkbox" name="size[]" id="size" value="Xl" />XL

Here The Example But I Want Same Thing In Html Or Php

http://gravitywiz./demos/limit-how-many-checkboxes-can-be-checked/

Problem Is Solved Now Here The Final Solution

<script src="http://ajax.googleapis./ajax/libs/jquery/1.9.1/jquery.min.js">
</script>

<script>
$(document).ready(function () {
$('input:checkbox').click(function(){
var $inputs = $('input:checkbox')
if($(this).is(':checked')){
$inputs.not(this).prop('disabled',true); 
}else{
$inputs.prop('disabled',false);
}
})
})
</script>

<input type="checkbox">
<input type="checkbox"> 
<input type="checkbox">
<input type="checkbox">
<input type="checkbox" />
Share Improve this question edited Apr 16, 2013 at 23:05 asked Apr 16, 2013 at 22:22 user2249079user2249079 8
  • 4 If you only want one selected, why not use radiobuttons instead? – icanc Commented Apr 16, 2013 at 22:24
  • 1 Edit: what @icanc said – Tomer Arazy Commented Apr 16, 2013 at 22:24
  • @icanc checkboxes is better then radio? – user2249079 Commented Apr 16, 2013 at 22:25
  • 2 It's not that one is better than another, they serve a different purpose. – Tomer Arazy Commented Apr 16, 2013 at 22:27
  • 1 Are you developing an app to show some students how not to do UX?! – Moby's Stunt Double Commented Apr 16, 2013 at 22:27
 |  Show 3 more ments

4 Answers 4

Reset to default 6
$("input[type='checkbox']").on("click" , function(){
    $("input[type='checkbox']").not(this).attr("disabled", "disabled");
});

Working Example

If you disable, user can't change his choice after first selection. Here is a radio button behavior for checkbox.

$("input[type=checkbox]").on('click', function() {   
    $("input[type=checkbox]").not(this).attr('checked', false);  

}); 

http://jsfiddle/BxF4Y/

But to doing that, the best way is to use radio buttons.

Browsing the source code of that page reveals the jQuery they used to achieve that effect. You should just be able to change the checkboxLimit to 1.

<script type="text/javascript">
jQuery(document).ready(function($) {

    $.fn.checkboxLimit = function(n) {

        var checkboxes = this;

        this.toggleDisable = function() {

            // if we have reached or exceeded the limit, disable all other checkboxes
            if(this.filter(':checked').length >= n) {
                var unchecked = this.not(':checked');
                unchecked.prop('disabled', true);
            }
            // if we are below the limit, make sure all checkboxes are available
            else {
                this.prop('disabled', false);
            }

        }

        // when form is rendered, toggle disable
        checkboxes.bind('gform_post_render', checkboxes.toggleDisable());

        // when checkbox is clicked, toggle disable
        checkboxes.click(function(event) {

            checkboxes.toggleDisable();

            // if we are equal to or below the limit, the field should be checked
            return checkboxes.filter(':checked').length <= n;
        });

    }


        $("#field_11_1 .gfield_checkbox input:checkbox").checkboxLimit(3);


});
</script>

Yes, this is an old thread, however there is no real conclusion here. I have included a fiddle with my version of how check-boxes should work should you wish to limit the number of boxes checked by the user.

/** Begin HTML **/
<div class="cContainer">
<div class="cDropdown">
    <div class="downArrow"></div>
    <h4>Night Life</h4>
</div>
<div class="multiCheckboxes stick">
    <input id="1" class="stick" type="checkbox" value="1" name="l-28">
    <label class="stick" for="1">Dive Bar</label>
    <br>
    <input id="2" class="stick" type="checkbox" value="2" name="l-28">
    <label class="stick" for="2">Pub</label>
    <br>
    <input id="3" class="stick" type="checkbox" value="3" name="l-28">
    <label class="stick" for="3">Dance Club</label>
    <br>
    <input id="4" class="stick" type="checkbox" value="4" name="l-28">
    <label class="stick" for="4">Pool Hall</label>
    <br>
    <input id="5" class="stick" type="checkbox" value="5" name="l-28">
    <label class="stick" for="5">Karaoke</label>
    <br>
    <input id="6" class="stick" type="checkbox" value="6" name="l-28">
    <label class="stick" for="6">Sports Bar</label>
    <br>
    <input id="7" class="stick" type="checkbox" value="7" name="l-28">
    <label class="stick" for="7">Trendy</label>
    <br>                    
</div>

/** END HTML **/

/** BEGIN JS **/

$('.cDropdown').on('click', function (e) {
$('.multiCheckboxes').slideUp(50)
e.stopPropagation();
currentDrop = $(this).next();
currentDrop.stop().slideToggle();
});
$('input:checkbox[name="l-28"]').on('change', function () {
var nightLifeLimit = $('input:checkbox[name="l-28"]:checked').length;
    if (nightLifeLimit == 2) {
    $('input:checkbox[name="l-28"]').each(function () {
        if ($(this).is(':checked')) {
            return;
        }
        else {
            $(this).prop('disabled', true);
        }
    });
}
else {
    $('input:checkbox[name="l-28"]').each(function () {
        $(this).prop('disabled', false);
    });
  }
});

/** END JS **/

/** BEGIN CSS **/

.cDropdown {
    background: none repeat scroll 0 0 #FFFFFF;
    border: 2px inset #D3D3D3;
    clear: right;
    padding: 4px 3px;
    width: 150px;
}
.cDropdown h4 {
    font-size: 0.86em;
    font-weight: normal;
    margin: 0 0 0 1px;
    padding: 0;
}
.downArrow {
    border-left: 8px solid rgba(0, 0, 0, 0);
    border-right: 8px solid rgba(0, 0, 0, 0);
    border-top: 8px solid #3C3C3C;
    float: right;
    height: 0;
    margin: 3px 0 0 3px;
    width: 0;
}
.multiCheckboxes {
    background: none repeat scroll 0 0 #FFFFFF;
    border: 1px solid #3C3C3C;
    display: none;
    left: 9px;
    max-height: 200px;
    overflow: auto;
    padding: 5px;
    position: absolute;
    top: 35px;
    width: 146px;
    z-index: 999;
}

.multiCheckboxes label {
   float: none !important;
    font-size: 0.9em;
    width: 7.6em !important;
}

http://jsfiddle/defmetalhead/9BYrm/1/

本文标签: How to limit checkbox selection to one using jquery or javascriptStack Overflow