【发布时间】:2018-05-08 08:45:56
【问题描述】:
我想根据复选框启用/禁用表单中字段的编辑。如果选中复选框,则必须启用其输入,如果未选中,则禁用。我为每个字段都有一些脚本代码,但我正在寻找通用代码而不是为每个字段编写代码。谁能帮助我使用 jquery 有效地实现这一目标。
$('#nCheck').click(function() {
$('input[name="Name"]').attr('disabled', false);
});
$('#pCheck').click(function() {
$('input[name="Position"]').attr('disabled', false);
});
$('#oCheck').click(function() {
$('input[name="Office"]').attr('disabled', false);
});
$('#aCheck').click(function() {
$('input[name="Age"]').attr('disabled', false);
});
$('#dCheck').click(function() {
$('input[name="sDate"]').attr('disabled', false);
});
$('#sCheck').click(function() {
$('input[name="Salary"]').attr('disabled', false);
});
$('#phCheck').click(function() {
$('input[name="Phone"]').attr('disabled', false);
});
$('#salCheck').click(function() {
$('input[name="ProSalary"]').attr('disabled', false);
});
$('#phoneCheck').click(function() {
$('input[name="ProPhone"]').attr('disabled', false);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form role="form" id="userForm" name="modalForm" action="" method="post">
<div class="form-group">
<label for="Name" class="field-label">Name <input type="checkbox" id="nCheck"/></label>
<input class="form-control" name="Name" id="person.name" value="Tiger Nixon" disabled/>
</div>
<div class="form-group">
<label for="Position" class="field-label">Position <input type="checkbox" id="pcheck"/></label>
<input class="form-control" name="Position" id="person.position" value="System Architect" disabled/>
</div>
<div class="form-group">
<label for="Office" class="field-label">Office <input type="checkbox" id="oCheck"/></label>
<input class="form-control" name="Office" id="person.office" value="Edinburgh" disabled/>
</div>
<div class="form-group">
<label for="Name" class="field-label">Age <input type="checkbox" id="aCheck"/></label>
<input class="form-control" name="Age" id="person.age" value="61" disabled/>
</div>
<div class="form-group">
<label for="Name" class="field-label">Start date <input type="checkbox" id="dCheck"/></label>
<input class="form-control" name="sDate" id="person.sDate" value="2011/04/25" disabled/>
</div>
<div class="form-group">
<label for="Name" class="field-label">Salary <input type="checkbox" id="sCheck"/></label>
<input class="form-control" name="Salary" id="person.salary" value="$320,800" disabled/>
</div>
<div class="form-group">
<label for="Name" class="field-label">Phone <input type="checkbox" id="phCheck"/></label>
<input class="form-control" name="Phone" id="person.phone" value="123-456-7890" disabled/>
</div>
<div class="modal-footer" id="modal-footer">
<button type="button" class="btn btn-primary save-btn" id="getDataBtn">Save changes</button>
</div>
</form>
请参考下面提到的 js fiddle 是我的表单 html - https://jsfiddle.net/ycf6Ltad/1/
【问题讨论】:
标签: jquery