2016-04-15 64 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

我更新了答案,現在它應該解決它的問題 –

相關問題