我想禁用Fieldset中的所有元素,但在其中启用了几个按钮。 Demo:
<fieldset ng-disabled="true">
<legend>Personalia:</legend>
Name: <input type="text"><br>
Email: <input type="text"><br>
Date of birth: <input type="text">
<input type="button" value="See More (Enable this!!) " ng-click="ButtonClicked1()" ng-disabled="false"/>
Somethingelse: <input type="text">
<input type="button" value="View Details " ng-click="ButtonClicked2()"/>
</fieldset>
试试这个:
HTML:
<fieldset id="form">
<legend>Personalia:</legend>
Name: <input type="text" /><br />
Email: <input type="text" /><br />
Date of birth: <input type="text" />
<input id="btn1" type="button" value="See More (Enable this!!) " onclick="ButtonClicked1()" />
Somethingelse: <input type="text" />
<input type="button" value="View Details " onclick="ButtonClicked2()"/>
</fieldset>
脚本:
$('#form :input').prop('disabled', true);
$("#btn1").prop('disabled', false);
我有2个解决方案:
<legend>
label {
display: block;
}
<fieldset disabled>
<legend>
Personalia:
<button>See more</button>
</legend>
<label>Name: <input></label>
<label>Email: <input></label>
<label>Date of birth: <input></label>
<label>Somethingelse: <input></label>
<button>View Details</button>
</fieldset>
你可以使用咏叹调角色来“伪造”按钮role="button"
(参见ARIA button role)。请记住添加必要的辅助功能,以便为没有显示屏幕或鼠标的用户单击此按钮。重要的属性是role="button"
(用于屏幕阅读器)和tabindex="0"
(用于键盘导航),还记得为用户没有鼠标时为Enter和Space添加keypress
的处理程序。
const disabledButton = document.querySelector('.disabled-button');
const disabledOutput = document.querySelector('.disabled-output');
const enabledButton = document.querySelector('.enabled-button');
const enabledOutput = document.querySelector('.enabled-output');
function increment(output) {
const current = Number.parseInt(output.textContent);
output.textContent = `${current + 1}`;
}
disabledButton.addEventListener('click', () => increment(disabledOutput));
disabledButton.addEventListener('keypress', event => {
if (['Enter', ' '].includes(event.key)) {
increment(disabledOutput);
}
});
enabledButton.addEventListener('click', () => increment(enabledOutput));
enabledButton.addEventListener('keypress', event => {
if (['Enter', ' '].includes(event.key)) {
increment(enabledOutput);
}
});
label {
display: block;
}
[role="button"] {
-webkit-appearance: button;
appearance: button;
cursor: default;
font-style: normal;
-webkit-user-select: none;
user-select: none;
}
<fieldset disabled>
<legend>Disabled form elements</legend>
<label>Input 1<input name="input 1"></label>
<label>Input 2<input name="input 2"></label>
<button
class="disabled-button"
name="disabled-button"
>
This is disabled
</button>
<i
class="enabled-button"
role="button"
tabindex="0"
>
This is enabled
</i>
</fieldset>
<label>
Disabled button clicks:
<output class="disabled-output">0</output>
</label>
<label>
Enabled button clicks:
<output class="enabled-output">0</output>
</label>