gpt4 book ai didi

javascript - 拒绝更改复选框的状态

转载 作者:行者123 更新时间:2023-11-28 19:29:49 25 4
gpt4 key购买 nike

我需要拒绝更改任何复选框的状态:

window.doNotChange = function(event) {
e = event || window.event;
if (e) {
e.stopPropagation();
e.preventDefault();
var t = $(e.target);
t.prop({checked: typeof t.attr('checked') != 'undefined'});
}
alert('Deny to change THIS checkbox!');
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p><label><input type="checkbox" checked onchange="doNotChange(event)"> name 1 (readonly)</label></p>
<p><label><input type="checkbox" onchange="doNotChange(event)"> name 2 (readonly)</label></p>
<p><label><input type="checkbox" checked> name 3 (editable)</label></p>
<p><label><input type="checkbox" checked onchange="doNotChange(event)"> name 4 (readonly)</label></p>
<p><label><input type="checkbox"> name 5 (editable)</label></p>
<p>etc.</p>

这段代码有效。但有更好的解决方案吗?

最佳答案

只需使用 HTML 的 disabled 属性即可:

<input type="checkbox" disabled />

关于javascript - 拒绝更改复选框的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27080192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com