2013-09-06 3 views
0

체크 표시가있는 여러 확인란 (예 : html 코드)을 사용하면 두 필드에 동일한 데이터가 표시됩니다. 그래서 간단한 예제는 "additional monitor"와 "visio"를 체크하고 "* Required"를 보여 주도록하는 것입니다.많은 확인란이 표시/숨기기를 전환합니다.

html로 코드 :

<table> 
<tr> 
    <td class="newemp_dataGLCCother"> 
     <label for="gl_account">GL Acccount:</label> 
     <input name="gl_account" id="gl_account" type="text" maxlength="15" /> 
     <label id="requiredgla" style="display:none"><font size="2" color="red">* Required</font> 
     </label> 
    </td> 
</tr> 
<tr> 
    <td class="newemp_dataGLCCother"> 
     <label for="cost_center">Cost Center:</label> 
     <input id="" name="cost_center" id="cost_center" type="text" maxlength="15" /> 
     <label id="requiredcc" style="display:none"><font color="red">*<font size="2"> Required</font></font> 
     </label> 
    </td> 
</tr> 
<tr> 
    <td> 
     <input type="checkbox" name="non_standard_software" value="Additional Monitor" id="additional_monitor" onclick="showReq('requiredgla'); showReq('requiredcc')" />Additional Monitor</td> 
</tr> 
<tr> 
    <td> 
     <input type="checkbox" name="non_standard_software" value="AutoCAD" id="autocad" onclick="showReq('requiredgla'); showReq('requiredcc')" />AutoCAD</td> 
</tr> 
<tr> 
    <td> 
     <input type="checkbox" name="non_standard_software" value="MapPoint" id="mappoint" onclick="showReq('requiredgla'); showReq('requiredcc')" />MapPoint</td> 
</tr> 
<tr> 
    <td> 
     <input type="checkbox" name="non_standard_software" value="Visio" id="visio" onclick="showReq('requiredgla'); showReq('requiredcc')" />Visio</td> 
</tr> 
<tr> 
    <td> 
     <input type="checkbox" name="non_standard_software" value="Project" id="project" onclick="showReq('requiredgla'); showReq('requiredcc')" />Project</td> 
</tr> 
<tr> 
    <td class="newemp_dataGLCCother"> 
     <input type="checkbox" name="non_standard_software" value="other" id="other" onclick="showReq('otherbox'); showReq('requiredgla'); showReq('requiredcc')" />Other: 
     <input name="other" id="otherbox" type="text" style="display:none;" /> 
    </td> 
</tr> 

자바 스크립트 :

function showReq(id) { 
var e = document.getElementById(id); 
if (e.style.display == 'block') 
    e.style.display = 'none'; 
else 
    e.style.display = 'block'; 
} 

그래서 당신은 "* 필수"항목의 홀수 번호를 확인 그대로이 코드로 표시되어 있지만 경우 표시되지 않은 짝수 개의 항목을 선택합니다.

그래서 변수를 "true"로 설정하고 js 함수에 여러 항목을 선택하면 토글을 제거 할 것이라고 생각했습니다.

예 :

function showReq(id) { 
var dbl = true; 
var e = document.getElementById(id); 
if (e.style.display == 'block' && dbl === true) 
    e.style.display = 'none'; 
dbl = false; 
else 
    dbl = true; 
    e.style.display = 'block'; 
} 

나는이 작동하지 않는다는 것을 알고 있지만 나는이 비슷한 찾고 있어요. JavaScript를 완전히 사용할 수 없다면 jQuery를 사용하지 마십시오.

+0

그래서 항상 표시하고 싶습니까? 'none'을 표시하기 위해 다시 설정하지 마십시오. 어떤 의미에서는 – PherricOxide

+0

입니다. 따라서 1/2/3/4/5/6을 확인하면 표시된 그대로 유지되지만 모두 선택 해제하면 숨겨져 야합니다. – Benny

답변

2

나는 당신이 달성하려고 시도하고 있다고 생각하는 것을 a fiddle을 통해 만들었습니다.

내가 알고 있듯이, 모든 체크 박스를 반복하여 점검 할 것인지 결정합니다. 둘 중 하나라도 선택되어 있으면 두 텍스트 상자가 모두 필요합니다.

showReq 대신 "필수"두 메시지의 표시 값을 동시에 설정하는 것이 더 효율적입니다.

내가 추가 한 방법은 showReq 내에서 호출되어 확인란을 선택했는지 확인하는 것입니다.

function checkboxesChecked() { 
    //get all the checkboxes 
    var checkboxes = document.getElementsByName('non_standard_software'); 
    //loop through checkboxes, if any are checked, return true 
    for (var i = 0; i < checkboxes.length; i++) 
     if (checkboxes[i].checked) return true; 
    return false; 
} 
+0

은 거의 똑같이 올릴 예정이었습니다 :) – andrew

+1

"다른"입력 상자가 선택 해제 된 후에 숨기지 만 그 이외의 모양을 사용합니다. – PherricOxide

+0

그것이 바로 내가 찾고있는 것입니다! 나는 for 루프에 대해 더 많이 읽을 필요가있다. 정말 고마워요. – Benny

관련 문제