2016-04-15 67 views
0

选中复选框时,我希望启用输入类型。 我已勾选复选框,但输入类型仍然禁用。 请帮助..复选框勾选时无法启用输入类型(jquery)

这是我的jQuery

$('#haha').change(
    function(id) { 
    if (this.checked) { 
     $("#nama_" + id).prop('disabled', false); 
     $("#ket_" + id).prop('disabled', false); 
    } else { 
     $("#nama_" + id).prop('disabled', true); 
     $("#ket_" + id).prop('disabled', true); 
    } 
    $(document).ready(function() { 
     $("input").focus(function() { 
     $(this).css("background-color", "yellow"); 
     }); 
     $("input").blur(function() { 
     $(this).css("background-color", "#lightblue"); 
     }); 
    }); 
    }); 

,这里是我的复选框,输入型

<td> <input type ="checkbox" id="haha" class=checkbox1 name="checklist" value=<?php echo $agenda->id; ?>> <?php echo $agenda->id; ?> </td> 
<td> <input type="text" name="dnama" id="nama_<?php echo $agenda->id; ?>" class="yes" value="<?php echo $agenda->nama; ?>" disabled /> </td> 
<td> <input type="text" name="dketer" id="ket_<?php echo $agenda->id; ?>" value="<?php echo $agenda->keterangan; ?>" disabled> </td> 
+0

你应该尝试 “删除” 属性 “已禁用”,没有将其设置为“false”... – Random

+1

为什么dom ready事件是在change事件中定义的。它应该与你正在做的相反 –

+0

我改变了它,但仍然一样。 。我认为问题在这里$(“#nama_”+ id)。如果我的输入类型名称只是“nama_”,并且$(“#nama_”+ id)我变成$(“#nama_”),它就会起作用。但我需要+ ID来获得不同的值 –

回答

0

首先使用点击事件,而不是变化事件复选框。

此外,为什么你在函数内部保存document.ready语句。 我的建议是把这个说法拿出来,并在里面做所有的事件绑定。

问题是您使用的变量id没有您想要的值。它包含其中的事件对象。 因此,要获取文本元素,您可以使用[type = text]选择器。 **仅当您仅有这些输入和复选框时才使用[type = text]选择器。如果您还有其他元素,请使用适当的选择器。代码如下所示。

$(document).ready(function() { 
    $("input[type=text]").focus(function() { 
     $(this).css("background-color", "yellow"); 
    }).blur(function() { 
     $(this).css("background-color", "#lightblue"); 
    }); 

    $('#haha').click(function() { 
     if ($(this).is(":checked")) { 
      $("input[type=text]").prop('disabled', false); 
     } else { 
      $("input[type=text]").prop('disabled', true); 
     } 
    }); 
}); 

好的具体的ID选择器,我可以从你的HTML看到,该ID包括复选框的值。所以你也可以使用它。

$(document).ready(function() { 
    $("input[type=text]").focus(function() { 
     $(this).css("background-color", "yellow"); 
    }).blur(function() { 
     $(this).css("background-color", "#lightblue"); 
    }); 

    $('#haha').click(function() { 
     var id_suffix = $(this).val(); 
     if ($(this).is(":checked")) { 
      $("input#name_"+id_suffix).prop('disabled', false); 
      $("input#ket_"+id_suffix).prop('disabled', false); 
     } else { 
      $("input#name_"+id_suffix).prop('disabled', false); 
      $("input#ket_"+id_suffix).prop('disabled', false); 
     } 
    }); 
}); 
+0

感谢您的答案,但是当我点击复选框时,所有输入类型都将启用。我想启用它,我只需点击 –

+0

它的工作原理,但启用输入类型只是第一行。 。当我取消选中第一个复选框并检查下一行时,输入类型仍然禁用。我认为问题来自$('#haha')。该ID不能相同。我该怎么办? –

+0

是的你是对的。两个元素的ID不应该相同。检查你是否犯了这个错误。 为此您可以使选择器具体化或在父级内搜索输入以禁用/启用 – Aditya

0
function enable(id) { 
if(document.getElementById('haha').checked) { 
document.getElementById("nama_"+id).disabled= false; 
document.getElementById("ket_"+id).disabled= false; 

}else { 
    document.getElementById("nama_"+id).disabled= true; 
    document.getElementById("ket_"+id).disabled= true; 
} 

}

0

这应该工作,你必须使用删除属性在道具去除disabled属性

$('#haha').change(function(event) { 
    var id = event.target.id; 
    if (this.checked) { 
     $("#nama_" + id).removeAttr('disabled');   $("#ket_" + id).removeAttr('disabled'); 
} else { 
    $("#nama_" + id).attr('disabled', true); 
    $("#ket_" + id).attr('disabled', true); 
} 
$(document).ready(function() { $("input").focus(function() { $(this).css("background-color", "yellow"); }); $("input").blur(function() { $(this).css("background-color", "#lightblue"); }); }); }); 
+0

当复选框检查我想输入从禁用状态启用。 –

+0

我更新了答案,现在它应该解决它的问题 –

相关问题