2012-10-02 2 views
0

두 개의 체크 박스 그룹이있어서 궁극적으로 그 중 하나가 다른 하나의 출력 (가시성)에 영향을 미칠 수 있습니다. 여기 내 피들입니다.동기화 체크 박스 가시성 값을 기반으로

http://jsfiddle.net/kEKYw/1/

따라서, 첫 번째 입력 (INT 값)의 값은 제 체크 박스 군 (현재 숨김)로부터의 입력 값과 일치해야하며, 그 선택에 기초하여 그것들을 나타낸다. 나는 가까이 다가 갈 수 있다고 느낀다. 그러나 올바른 방향으로 향한 움직임은 엄청난 도움이 될 것이다. 감사!

+0

귀하의 예를 찾고있는 것을 모두 체크 박스 그룹은 동일한 값을 포함로 나에게 분명하지 않다 생각합니다. 어느 쪽이든, 당신의 jQuery selector는'wood_types'의 id를 가진 요소가 없기 때문에 요소를 찾지 못합니다. 시작 또는 포함 선택기 : http://api.jquery.com/category/selectors/를 사용할 수 있습니다. –

답변

1

코드가 약간 수정되었습니다. 코드에 대한 의견을 참조하십시오.

DEMO는 :http://jsfiddle.net/kEKYw/3/

$("#wood_typeschecklist input").change(function() { 
    var destEl = this.id.split("-"); 
    //a. Changed selector to look for starts with selector as the 
    //checkbox value returned is wood_type-<number> 
    //which doesn't match any existing id 

    //b. `.split` splits the string when it matches its 
    //separator and so it was returning 3 tokens not 2 (in,wood_types,126) 
    var $matchingElem = $("li[id^=" + destEl[1] + '-' + destEl[2] + "]", $("#acf-wood_species_availability")); 
    ($(this).attr("checked") !== undefined) ? $matchingElem.show() : $matchingElem.hide(); 
}); 
+0

매우 시원한 - 테스트하고보고합니다. 고맙습니다. – Zach

+0

정확히 내가 무엇을 찾고 있었습니까. 나는 당신이 pageload에 이미 "선택"되어 있는지 확인하기 위해 추가로 제공 한 것의 http://pastebin.com/mXbVgkK9를 작성했다. 다시 한 번 감사드립니다! – Zach

1

나는 this fiddle 당신이

// start off by hiding the second options 
$("#acf-wood_species_availability li").hide(); 

//add a click method to the inputs 
$("#wood_typeschecklist input").click(function() { 
    //verify that it's checked, if so, show the other input of the same value (and check it also) 
    if ($(this).is(':checked')) { 
     $('#acf-wood_species_availability input[value="' + $(this).val() + '"]').attr('checked', true).parents('.popular-category').show(); 
    } 
    //if it's unchecked, hide the other input and uncheck this one 
    else { 
     $('#acf-wood_species_availability input[value="' + $(this).val() + '"]').attr('checked', false).parents('.popular-category').hide();        
    } 
}); 

//now let's add click methods to the other checkboxes in case user clicks on them, if so, we hie this checkbox and uncheck the other one. 
$("#acf-wood_species_availability input").click(function() { 
    $(this).attr('checked', '').parents('.popular-category').hide(); 
    $('#wood_typeschecklist input[value="' + $(this).val() + '"]').attr('checked', false); 
}); 
+0

감사합니다. @Vega가 처음에 도착했습니다. 다시 한 번 감사드립니다! – Zach

+0

사실,하지만 사용자가 두 가지 방법을 모두 사용할 수 있도록하고 싶다고 생각했습니다. 그래서 내 코드가 좀 더 복잡해졌습니다! 건배! –

관련 문제