如何在 jquery 中将下拉菜单设置为只读?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16713436/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:38:51  来源:igfitidea点击:

how can i set the drop down as readonly in jquery?

javascriptjqueryhtmlasp.net-mvc-4

提问by Niths

I want to set the drop down as readonly. For that I user the code readonly="readonly".But its not working in asp.netmvc4.How can I solve this problem?Please help

我想将下拉菜单设置为只读。为此,我使用了代码readonly="readonly"。但它在 asp.netmvc4 中不起作用。我该如何解决这个问题?请帮忙

采纳答案by PSR

You can use .attr()in jQuery

您可以.attr()在 jQuery 中使用

$('#id').prop("disabled", true); 

回答by Dhaval Marthak

You can do like this

你可以这样做

Make other options disabled except the current one.

禁用除当前选项之外的其他选项。

$('option:not(:selected)').attr('disabled', true);

See Demo

看演示

回答by edd

It can be done via jquery. It saves you from browser compatibility issues too. jQuery:

它可以通过jquery完成。它也可以避免浏览器兼容性问题。jQuery:

$('#yourselectid').attr("disabled", true);

回答by johnkavanagh

Unfortunately the select element doesn't accept readonly, W3 spec is here for reference.

不幸的是,select 元素不接受只读,W3 规范在这里供参考

As others have suggested, your best bet would be to use the disabled 'disabled' attribute instead.

正如其他人所建议的那样,最好的办法是使用禁用的“禁用”属性。