我正在尝试实现以下表单功能的反转。这与我想要的功能相反。我希望默认情况下禁用表单元素,然后在按下“点击器”时启用表单元素。我正在尝试以下代码但没有成功。我对 HTML 没有任何问题,我的问题是让脚本按照我想要的方式运行。
示例 HTML 表单元素
<input type='text'></input>
<input type='text'></input>
<input type='text'></input>
<div id='clicker' style='background-color:#FF0000; height:40px; width:100px;'></div>
这是我正在尝试的 JavaScript:
$().ready(function() {
$('#clicker').click(function() {
$('input').each(function() {
if ($(this).attr('disabled')) {
$(this).removeAttr('disabled');
}
else {
$(this).attr({
'disabled': 'disabled'
});
}
});
});
});
最佳答案
对于禁用
,应使用prop()
而不是attr()
。
您还可以使用 prop(propertyName, fn)
创建循环并隔离实例
$(function () {
inputs_toggle_disable();//disable on page load, assumes none have disabled in markup
$('button').click(inputs_toggle_disable);
});
function inputs_toggle_disable() {
$('input').prop('disabled', function () {
return !this.disabled
});
}
关于javascript - 如何反转以下表单元素禁用功能的过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30150845/