当每个框的值相同时,为什么button
未被禁用。
我有4个文本框,2个用于比较部门名称的文本框和2个用于比较部门活动的文本框。
$("#departmentIDInput, #departmentNameInput, #departmentActive").bind("keyup change", function() {
var departmentIDInput = $("#departmentIDInput").val();
var departmentNameInput = $("#departmentNameInput").val();
var departmentNameInputCurrent = $("#departmentNameInputCurrent").val();
var departmentActive = $("#departmentActive").val();
var departmentActiveVal = $("#departmentActiveVal").val();
var departmentActiveValCurrent = $("#departmentActiveValCurrent").val();
if (departmentNameInput != departmentNameInputCurrent) {
$("#btnUpdate").attr('disabled', false).removeClass('btn_inact').addClass('btn_act');
} else {
$("#btnUpdate").attr('disabled', true).removeClass('btn_act').addClass('btn_inact');
}
if (departmentActiveVal == departmentActiveValCurrent) {
$("#btnUpdate").attr('disabled', false).removeClass('btn_inact').addClass('btn_act');
} else {
$("#btnUpdate").attr('disabled', true).removeClass('btn_act').addClass('btn_inact');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="departmentNameInput" name="departmentNameInput" maxlength="30" value="Production"/>
<input type="text" id="departmentNameInputCurrent" name="departmentNameInputCurrent" maxlength="30" value="Production"/>
<input type="text" name="departmentActiveVal" class="departmentActiveVal" id="departmentActiveVal" value="Y"/>
<input type="text" name="departmentActiveValCurrent" class="departmentActiveValCurrent" id="departmentActiveValCurrent" value="Y"/>
<input type="checkbox" class="departmentActive" id="departmentActive" />
<input type="button" class="button turquoise" id="btnUpdate" value="Update" disabled/>
有人有想法吗?
答案 0 :(得分:0)
由于您的第二个if条件,每个框的值相同时,按钮不会被禁用。
if (departmentActiveVal == departmentActiveValCurrent) {
$("#btnUpdate").attr('disabled', false).removeClass('btn_inact').addClass('btn_act');
}
此计算结果为true,按钮将启用
答案 1 :(得分:0)
您可以尝试在三元操作中简化您的功能,例如:
$("#departmentIDInput, #departmentNameInput, #departmentActive").bind("keyup change", function() {
var departmentIDInput = $("#departmentIDInput").val();
var departmentNameInput = $("#departmentNameInput").val();
var departmentNameInputCurrent = $("#departmentNameInputCurrent").val();
var departmentActive = $("#departmentActive").val();
var departmentActiveVal = $("#departmentActiveVal").val();
var departmentActiveValCurrent = $("#departmentActiveValCurrent").val();
var isNameEqual = departmentNameInput === departmentNameInputCurrent;
var isActiveValueEqual = departmentActiveVal === departmentActiveValCurrent;
var isDisabled = isNameEqual && isActiveValueEqual;
$("#btnUpdate").prop('disabled', isDisabled).removeClass('btn_inact').addClass('btn_act');
});