jQuery Jquery在选择上添加/删除多个属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5881626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery add / remove multiple attribute on select
提问by John
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 this using jquery?
我想根据上面的复选框更改用户是否只能在选择框中选择一个或多个选项。即,如果勾选复选框,用户可以选择多个值,如果不勾选,则只能选择一个值。使用 jquery 执行此操作的最佳方法是什么?
采纳答案by karim79
$("#theCheckbox").change(function() {
$("#theSelect").attr("multiple", (this.checked) ? "multiple" : "");
}).change();
回答by Csaba Toth
For newer jQuery setting prop instead of attr works:
对于较新的 jQuery 设置道具而不是 attr 作品:
http://jsfiddle.net/DdhSF/267/
http://jsfiddle.net/DdhSF/267/
$("#theCheckbox").change(function() {
$("#theSelect").prop("multiple", (this.checked) ? "multiple" : "");
}).change();
回答by YNhat
$('#checkboxid').change(function(){
if ($(this).is(':checked'))
{
$('#listbox').attr('multiple','multiple');
}
else
{
$('#listbox').removeAttr('multiple');
}
})