2014-06-18 28 views
-1

我有一個動態創建的複選框列表,每個複選框都有輸入字段,所以如果輸入字段值爲零,我想禁用複選框。下面是我的代碼如果輸入值爲零,則禁用複選框

HTML代碼

<input class="check" id="check" name="check" type="checkbox"><label for="check">checkbox <input type="hidden" id="test" value="0" /></label> 
<input class="check" id="check1" name="check1" type="checkbox"><label for="check1">checkbox1 <input type="hidden" id="test" value="6" /></label> 

jQuery的

if($("#test").val() == "0"){ 
    $('.check').attr("disabled", "disabled"); 
} 

jsfiddle

+0

請解釋什麼是不工作的。 – Scimonster

+3

你在同一頁面上使用兩次相同的ID! –

+0

正如你所說「如果輸入字段值爲零,你想禁用複選框」,你的小提琴也是這樣做的。那麼什麼問題 –

回答

2

您使用兩次id=test和JavaScript的工作,只有他們的第一個。

id=test更改爲class=test,或與兩個不同id s一起使用。

工作代碼:

$('.test').each(function() { 
    if ($(this).val() == '0') { 
     $(this).parent().prev().attr('disabled', 'disabled'); 
    } 
}); 

http://jsfiddle.net/cDD5L/

1
$("input[type=hidden]").each(function() { 
    if($(this).val()==0){ 
    $(this).parent().prev().attr('disabled', 'disabled'); 
}        
}); 

演示:

http://jsfiddle.net/SxypS/

0

請使用

$(function() { 
    enable_cb(); 
    $("#group1").click(enable_cb); 
}); 

function enable_cb() { 
    if (this.checked) { 
    $("input.group1").removeAttr("disabled"); 
    } else { 
    $("input.group1").attr("disabled", true); 
    } 
} 

它會爲你工作。

讓我知道如果還是不行

相關問題