<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 7 年前。
标签 javascript jquery html css
<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 7 年前。
我已尝试使用 JQuery 和 JavaScript 更改标签的 CSS 以使其变灰。但是我没有成功。
标签位于复选框旁边,我可以禁用复选框但不能禁用旁边的标签。
标签创建如下: 特别的
我想在有人点击 ID 为“idRadio”的单选按钮时更改此标签。
最佳答案
您不需要 jQuery 或 Javascript 来完成您想要实现的目标。
如果您的 <label>
标记放在 <input type=checkbox>
旁边第一,你可以使用 CSS 选择器:
/* gets the label that is immediately after a disabled input type=checkbox */
input[type=checkbox]:disabled+label {
/* and changes its color to gray */
color: gray;
}
document.addEventListener('DOMContentLoaded', function() {
document.querySelector('button').addEventListener('click', function(e) {
var cb = document.getElementById('cb');
cb.disabled = !cb.disabled;
});
});
input[type=checkbox]:disabled+label {
color: gray;
}
<div>
<input id='cb' type='checkbox' disabled> <label for='cb'>My checkbox</label>
</div>
<div>
<button>Toggle disabled</button>
</div>
已更新
根据您的评论,我已经实现了一个 Javascript 版本来执行您想要的操作,但我根本不推荐它。我只是想在这里帮助你:
document.addEventListener('DOMContentLoaded', function() {
var radios = document.getElementsByName('idRadio');
[].forEach.call(radios, function(radio, i) {
radio.addEventListener('change', function() {
var sp = document.getElementById('idSpecial');
sp.disabled = (this.value === "0");
sp.parentNode.removeChild(sp.nextSibling);
var span = document.createElement('span');
if (sp.disabled) span.style.color = 'gray';
span.textContent = 'Special';
sp.parentNode.insertBefore(span, sp.nextSibling);
});
});
});
<input type="checkbox" id="idSpecial" name="Special">Special
<input type="radio" value="1" name="idRadio" checked>Enable</input>
<input type="radio" value="0" name="idRadio">Disable</input>
关于javascript - 如何使用 HTML 表单的输入字段禁用标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32707768/
相关文章:
javascript - .remove() 在成功函数上不使用 ajax
javascript - jQuery 未定义 - WordPress footer.php
javascript - 在 javascript/jquery 中平移 div
javascript - 如何使用 react-spring 创建视差固定 header
javascript - 无法使突出显示 js 在 meteor js 中工作
jquery - Bootstrap 下拉菜单不适用于 SlidesJS 3.0