2016-09-06 3 views
0

데이터베이스로부터 데이터를 가져 오는 목록이 있고 선택란에서 입력 필드를 활성화하고 싶습니다. 그러나 첫 번째 라인에서만 작동합니다.체크 박스가 체크 박스에 체크 된 후 입력 필드를 활성화하십시오.

<?php 
    require_once('inc/config.php'); 
    include 'verification/verify_form_details.php'; 
?> 
<html> 
<head> 
    <title>getElementById example</title> 
    <script type="text/javascript" src="js/jquery-2.1.3.min.js"></script> 
</head> 
<body> 
    <?php 
     $result2 = getProcessID(); 
    ?> 
    <label>Process: <small style="color:red;">*</small>    
    <?php 
     while ($row = mysql_fetch_array($result2)) 
     { 
      $row[0] = cleanOutputData($row[0]); 
    ?>  
    <div>  
     <label> 
     <input type="checkbox" class="checkbox" name="process[]" id=<?php echo $row[0] ?> value=<?php echo $row[0]?> /><?php echo $row[0] ?> 
     </label>           

     <label> 
     <input type="text" class="field" disabled name="number[]" id=<?php echo $row[0] ?> /> 
     </label> 

    </div> 
    <?php 
     } 
     mysql_free_result($result2); 
    ?> 
    </label> 
</body> 
</html> 

그리고 자바 스크립트 함수 :

<script> 
    $(document).ready(function() { 
      $('.checkbox').change(function() { 
       $('.field').attr('disabled',!this.checked) 
      }); 
    }); 
</script> 

어떤 아이디어를 내가 그것을 할 수있는 방법은 다음 HTML 코드? 당신이

+0

때문에 같은'ID' 사용 클래스를 참조 부탁드립니다. ID는 고유해야합니다 – guradio

+0

@guradio 클래스를 시도한 후 하나의 확인란을 눌러 모든 필드를 활성화했습니다. 내 질문도 편집 –

+0

'$ (this) .parent(). next(). find ('. field'). attr ('disabled',! this.checked)' – guradio

답변

4

$('.checkbox').change(function() { 
 
    $(this).parent().next().find('.field').prop('disabled', !$(this).is(':checked')) 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<label> 
 
    <input type="checkbox" class="checkbox" name="process[]" id=v alue=/> 
 
</label> 
 

 
<label> 
 
    <input type="text" class="field" disabled name="number[]" id=/> 
 
</label> 
 

 

 
<label> 
 
    <input type="checkbox" class="checkbox" name="process[]" id=v alue=/> 
 
</label> 
 

 
<label> 
 
    <input type="text" class="field" disabled name="number[]" id=/> 
 
</label> 
 

 

 
<label> 
 
    <input type="checkbox" class="checkbox" name="process[]" id=v alue=/> 
 
</label> 
 

 
<label> 
 
    <input type="text" class="field" disabled name="number[]" id=/> 
 
</label>

  1. 사용 클래스 대신 ID
  2. 사용이 컨텍스트 대신 변경된 요소
관련 문제