2013-02-08 51 views
2

我嘗試使用jquery啓用複選框時啓用文本框。 my page 的jQuery:勾選複選框時啓用文本框

<script> 
$(document).ready(function(){ 
    $('#isNews').change(function(){ 
    $("#newsSource").prop("disabled",false); 
}); 
}); 
</script> 

HTML:

<label class="checkbox"> 
    <input type="checkbox" id="isNews" name="isNews">If you want send news: 
</label> 
<label for="newsSource" class="ilable">news source</label> 
<input id="newsSource" name="newsSource" class="input-xlarge" disabled="" type="text" placeholder="news source"> 

什麼問題?

回答

11

更改爲

$('#isNews').change(function(){ 
    $("#newsSource").prop("disabled", !$(this).is(':checked')); 
}); 

演示:Fiddle

+0

它沒有工作。你能看到我的頁面嗎?請告訴 – Ehsan

+0

@Ehsan你需要使用'change'事件,因爲給定'$('#isNews')。change(function(){(「#newsSource」)。prop(「disabled」,!$(this) .is(':checked')); });' –

+0

謝謝我的老闆 – Ehsan

2

我不說阿拉伯語,但你的jQuery包括後所有腳本的其餘部分。你需要包含jQuery 第一個

您在控制檯中顯示「Undefined variable $」。一定要注意控制檯。

+0

+1 !!!!你總是很快... :) :) – bipen

+0

我把jQuery放在標題中,並從頁腳中刪除它,但它不起作用 – Ehsan

+0

@Ehsan你也刪除了'.change'事件 –

0

你有

$ is not defined 

所以你probabaly沒有帶裝jQuery的錯誤.. :)

<head>標籤添加此

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
+0

我把jquery放在了腦中,但是它不工作 – Ehsan

0

這裏是您正確的代碼

<script> 
$(document).ready(function(){ 
    $('#isNews').click(function(){ 
    if($(this).is(":checked")) 
     $("#newsSource").removeAttr("disabled"); 
    else 
     $("#newsSource").atte("disabled" , "disabled"); 
}); 
}); 
</script> 
4

除了涉及非包容的jQuery的錯誤,你的代碼改成這樣:

$('#isNews').change(function() { 
    $("#newsSource").prop("disabled", !this.checked); 
}); 
0

我打開你所給出的鏈接。在你的頁面源代碼中,問題在於你在jquery代碼之後加入了jquery。嘗試在document.ready代碼之前添加它。

+0

我包括jQuery中的標題,但問題沒有解決 – Ehsan

相關問題