【发布时间】:2016-06-08 06:54:34
【问题描述】:
我遇到了一个问题,我需要您的帮助。我已经创建了两个单独的表单。第一个有两个输入文本,第二个有一些复选框。我希望在加载页面上禁用第二个并且当一个复选框是选中然后表单将被启用,第一个将被禁用。如果没有选中复选框,则第一个被启用,第二个被禁用。希望你理解。我的英语不完美..
这是我使用的 jQuery:
//eksargirwsh is the 2nd form (with checkboxes)
//prosthiki is the 1nd form (with two input text)
$(document).ready(function() {
$('#eksargirwsh :not([type=checkbox])').prop('disabled', true);
});
$(":checkbox").change(function(){
if (this.checked) {
$('#eksargirwsh :not([type=checkbox])').prop('disabled', false);
$('#prosthiki').prop('disabled', true);
}
else{
$('#eksargirwsh :not([type=checkbox])').prop('disabled',true);
$('#prosthiki').prop('disabled', false);
}
}) ;
错误
- 在加载页面上,第二个没有按预期禁用
- 如果我在一行中选中了两个或更多的 checkbx,并以相反的方式取消选中它们,那么第二个表单将变为禁用事实,我不想要
这是我找到的解决方案:
$(document).ready(function() {
$('#eksargirwsh :not([type=checkbox])').removeAttr('disabled');
});
$(":checkbox").change(function(){
//if (this.checked){
if ($('form input[type=checkbox]:checked').size()>=1){
$('#eksargirwsh :not([type=checkbox])').removeAttr('disabled');
$('#prosthiki').prop('disabled', true);
}
else{
$('#eksargirwsh :not([type=checkbox])').prop('disabled',true);
$('#prosthiki').removeAttr('disabled');
}
});
我把它放在输入上:
disabled="disabled"
【问题讨论】:
-
你应该使用
.removeAttr('disabled')。请检查我的答案。 -
你能提供一个jsfiddle吗?
-
表单是php生成的,无法提供!
标签: javascript jquery forms checkbox