如何制作 html <选择>元素看起来像“禁用",但传递值? [英] How to make html &lt;select&gt; element look like &quot;disabled&quot;, but pass values?

查看:20
本文介绍了如何制作 html <选择>元素看起来像“禁用",但传递值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我禁用一个

<select name="sel" disabled>
    <option>123</option>
</select>

元素,它不传递它的变量.

element, it doesnt pass its variable.

怎么做才能看起来像残疾人,但处于正常"状态?

What to do to look it like disabled, but be in "normal" state?

这是因为我有一个选择"列表,有时其中一些只有一个值,所以用户应该明白它只有一个值,而不需要点击它.

This is because I have a list of "selects", and sometimes some of them have single value, so user should understand that it has only one value without clicking it.

推荐答案

您可以根据需要保持禁用状态,然后在提交表单之前移除禁用属性.

You can keep it disabled as desired, and then remove the disabled attribute before the form is submitted.

$('#myForm').submit(function() {
    $('select').removeAttr('disabled');
});

请注意,如果您依赖此方法,您还需要以编程方式禁用它,因为如果 JS 被禁用或不受支持,您将被禁用的选择卡住.

Note that if you rely on this method, you'll want to disable it programmatically as well, because if JS is disabled or not supported, you'll be stuck with the disabled select.

$(document).ready(function() {
    $('select').attr('disabled', 'disabled');
});

这篇关于如何制作 html <选择>元素看起来像“禁用",但传递值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆