开发者

Jquery add / remove multiple attribute on select

I want to change whether a user can select only, one or multiple options in a select box based on a tick box above. i.e. if the tick box is ticked the user can select multiple values, if not ticked they can only select one value. What is the best way to do t开发者_如何学编程his using jquery?


$('#checkboxid').change(function(){
    if ($(this).is(':checked')) {
        $('#listbox').attr('multiple','multiple');
    } else {
        $('#listbox').removeAttr('multiple');
    }
})

Demo: http://jsfiddle.net/ynhat/WCPue/1/


For newer jQuery setting prop instead of attr works:

http://jsfiddle.net/DdhSF/267/

$("#theCheckbox").change(function() {
    $("#theSelect").prop("multiple", (this.checked) ? "multiple" : "");
}).change();


$("#theCheckbox").change(function() {
    $("#theSelect").attr("multiple", (this.checked) ? "multiple" : "");
}).change();

You can try it here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜