2014-05-13 1 views
0

91 개의 체크 박스와 91 개의 숨겨진 요소가 있습니다. 숨겨진 요소와 값이 0 인 루프를 반복하려고하면 해당 확인란이 비활성화됩니다. 템플릿 변수는 페이지가 제공되기 전에 0 또는 1로 대체됩니다.jQuery는 숨겨진 필드의 값에 따라 체크 박스를 비활성화합니다.

은 여기 내 마크 업의 일부입니다 : JQuery와의

<input type="hidden" class="h_status" name="enabled_25" value="0"> 
<br><input type="checkbox" class="c_status" name="perm_25" value="1"> Create RPL Invoice 

<input type="hidden" class="h_status" name="enabled_50" value="0"> 
<br><input type="checkbox" class="c_status" name="perm_50" value="1"> Uncancel Invoice 

<input type="hidden" class="h_status" name="enabled_49" value="0"> 
<br><input type="checkbox" class="c_status" name="perm_49" value="1"> Flag As Fraud 

<input type="hidden" class="h_status" name="enabled_54" value="1"> 
<br><input type="checkbox" class="c_status" name="perm_54" value="1"> Change Initials 

<input type="hidden" class="h_status" name="enabled_3" value="1"> 
<br><input type="checkbox" class="c_status" name="perm_3" value="1"> View/Edit CC Info 

<input type="hidden" class="h_status" name="enabled_52" value="1"> 
<br><input type="checkbox" class="c_status" name="perm_52" value="1"> View Invoice History 

<input type="hidden" class="h_status" name="enabled_47" value="1"> 
<br><input type="checkbox" class="c_status" name="perm_47" value="1"> Reprint Pull Copy 

내 초기 시도는 작동하지 않습니다 (체크 박스 중 어느 것도 사용할 수 없음). 각 함수와 h_status 선택기를 사용하여 숨겨진 요소를 반복합니다. 그런 다음, 요소의 값이 공백이거나 0이면 c_status 선택기를 사용하여 선택란을 사용 불가능하게하십시오. 여기

은 (난 아직 배우는 중이에요 부드러운주세요) 내 코드는 지금까지의 :

$('.h_status').each(function(){ 

    var tempval = $(this).val(); 

    if (tempval == "" || tempval == 0){  
     $('.c_status').prop('disabled', true);  
    } 
}); 
+0

사용 작은 따옴표는 = 는 방법이 –

+0

<이름 tmpl_var = XYZ> 가져 "이름 옆에 = 새로운 시작 따옴표 또는 =이 값에 대한 닫는 따옴표"만일 알고 가정 파서이다 0 또는 1로 바뀌므로 숨겨진 필드에 대해 value = "0"또는 value = "1"로 페이지가 제공됩니다. – VinnyTony

+0

결과는 무엇입니까? 첫 번째 확인란은 항상 비활성화되어 있지만 다른 것은 아닙니다. – Samurai

답변

0

아마이 작동합니다 :

$('.h_status').each(function(){ 
    var d = $(this).val() == 0 ? false : true; 
    var name = $(this).attr('name').split('_'); 
    $('input[name="perm_'+name[1]+'"]').prop('disabled', d); 
}); 

면책 조항 : 검증되지 않은

+0

로직을 따라갈 수 있지만 콘솔에 다음과 같이 표시됩니다. TypeError : 이름이 함수가 아닙니다 – VinnyTony

+0

@VinnyTony 죄송합니다. 답안에 오타가 있었지만 지금은 해결되었습니다. – zgood

+0

$ (this) .val() == 0을 $ (this) .val()! = 0으로 변경했고 작동합니다! 나는 이것을 투표 할 것이지만 나는 더 나은 대표를 필요로한다. :) – VinnyTony

0

이후 당신은 입력/체크 박스를 함께 포함하지 않았고, 당신이하려는 것은 1 대 1의 관계라고 생각합니다. 당신은 somethi 적합한 체크 박스를 얻으려면 .next()을 클릭하십시오. 당신의 가치

$('.h_status').each(function(){ 
    var thisInput = $(this).attr('name'); 
    var thisInputNum = thisInput.split("_")[1]; 
    var thisVal = $(this).val(); 
    if(!thisVal){ 
     $('.c_status[name="perm_' + thisVal + '"]').prop('disabled', true); 
    } 
}); 
관련 문제