在 html 中禁用/只读选择标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8311278/
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
Disable/readonly select tag in html
提问by Muhammad Saifuddin
How exactly can I go about making a select tag readonly. I've read that you can't set a select as readonly. I do that I can set it as disabled but... That is not an option. I would like the user to view the rest of the options in the select tag, but not be able to pick one of them..
我究竟如何才能将选择标签设为只读。我读过您不能将选择设置为只读。我这样做,我可以将其设置为禁用,但是...这不是一个选项。我希望用户查看选择标签中的其余选项,但无法选择其中之一。
Any ideas?
有任何想法吗?
回答by Muhammad Saifuddin
you can use disabled attribute.
您可以使用禁用属性。
for example.
例如。
<select >
<option disabled="disabled" value="volvo">Volvo</option>
<option disabled="disabled" value="saab">Saab</option>
<option disabled="disabled" value="mercedes">Mercedes</option>
<option disabled="disabled" value="audi">Audi</option>
</select>
回答by armen
assume that tag id="mySelectID", and jQuery is resident, then :
假设标签 id="mySelectID",并且 jQuery 是常驻的,那么:
<script language="javascript">
// disable all the options that are not in use:
$("#mySelectID option").not(":selected").attr("disabled", "disabled");
// to remove readonly, enable them again:
$("#mySelectID option").not(":selected").attr("disabled", "");
</script>
回答by Kim Jensen
You can make it fake-readonly by adding style="pointer-events:none;"
您可以通过添加使其成为假只读 style="pointer-events:none;"
回答by Murali Ayyagari
I used the selected disabled values for the option tag.
我为选项标签使用了选定的禁用值。
It will serve the purpose of prompting the user to select an option and at the same time disabling that option.
它将用于提示用户选择一个选项并同时禁用该选项。
Example:
例子:
<option selected disabled>-Select-</option>
回答by ahll
https://coderanch.com/t/469173/select-box-READONLY-true
https://coderanch.com/t/469173/select-box-READONLY-true
if you want to disable the select box, then you can use a javascript code to enable it back before the form submits. So on the onsubmit event of the form, you can enable the select box so that its value is also submitted when the form is submitted...
如果您想禁用选择框,那么您可以使用 javascript 代码在表单提交之前将其重新启用。所以在表单的onsubmit事件上,可以启用选择框,这样在提交表单的时候也提交了它的值...