Javascript 如何使复选框不可点击*而不*它在浏览器中变灰?

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

How do I make a checkbox unclickable *without* it being greyed out in the browser?

javascriptjqueryhtmlcheckboxclickable

提问by B Seven

How do you create an HTML checkbox that is unclickable, but not greyed out? I used the disabled=disabled tag, but that makes it greyed out (in Chrome). Otherwise it works well. Working in jQuery and Rails...

如何创建一个不可点击但不灰显的 HTML 复选框?我使用了 disabled=disabled 标签,但这使它变灰(在 Chrome 中)。否则它运作良好。在 jQuery 和 Rails 中工作...

Thanks.

谢谢。

回答by Andrew Whitaker

Usability concerns aside:

撇开可用性问题不谈:

$("input:checkbox").click(function() { return false; });

For example: http://jsfiddle.net/nKwRj/

例如:http: //jsfiddle.net/nKwRj/

回答by Salman A

Use jQuery.onwith falseas the callback:

使用jQuery.onwithfalse作为回调:

$(":checkbox").on("click", false);

Fiddle

小提琴

回答by user3337728

Prevent checkboxes from being changed by ID or by Class.

防止复选框被 ID 或类更改。

/* by class */
$(".nochange").click(function () {
    return false;
});
/* by ID */
$("#nochange").click(function () {
    return false;
});

<input name='test' type='checkbox' class='nochange' checked='checked' />
<br />
<input name='test' type='checkbox' id='nochange' checked='checked' />

http://jsfiddle.net/mjames757/nX64E/1/

http://jsfiddle.net/mjames757/nX64E/1/

回答by Stein G. Strindhaug

Why do you want to make a unclickable checkbox appear to be clickable? To fool the users?

为什么要使不可点击的复选框看起来是可点击的?欺骗用户?

If you really want to fool the users, you could place a div with 0.01 opacity above it using absolute positioning (no script required). But I still wonder why you feel the need to do this prank on your users... ;)

如果您真的想欺骗用户,您可以使用绝对定位(不需要脚本)在其上方放置一个不透明度为 0.01 的 div。但我仍然想知道为什么你觉得有必要对你的用户做这个恶作剧......;)

EDIT: If what you really need is to include a value the user should not change but you don't want what you probably are considering "an ugly disabled checkbox", you should use input type hidden, which is completely invisible.

编辑:如果您真正需要的是包含用户不应更改的值,但您不想要您可能正在考虑的“丑陋的禁用复选框”,则应使用input type hidden完全不可见的 。

If you want to indicate that something is preselected but not changeable or something like that, use a disabled checkbox to indicate this (or an image of a pretty checkbox if you like), but beware that the value of a disabled checkbox is not submitted. Use a hidden field to include the the data needed.

如果您想表明某些内容已预先选择但不可更改或类似的内容,请使用禁用复选框来表明这一点(如果您愿意,也可以使用漂亮的复选框的图像),但要注意禁用复选框的值不会提交。使用隐藏字段来包含所需的数据。

回答by Morten Brockhoff

I ran into this post in search of an answer to the initial question - and found the opposals to non-grayed disabled checkboxes convincing, so instead i made a td-tag containing this: <%if rs("yes/no value") = -1 then response.write("√")%> (wing is a &radic). Then i get a clean info wing not inviting to click.

我跑到这篇文章中寻找最初问题的答案 - 并发现对非灰色禁用复选框的反对令人信服,所以我制作了一个包含以下内容的 td 标签:<%if rs("yes/no value") = -1 然后 response.write("√")%> (wing 是一个 &radic)。然后我得到一个干净的信息翼,不邀请点击。