2014-10-14 3 views
0

jQuery에서 PHP 변수를 사용하고 싶습니다.
텍스트 상자와 라디오 버튼이있는 테이블에 50 명의 학생 데이터가 있습니다.
기본적으로 텍스트 상자가 숨겨져 있지만 라디오 버튼 텍스트 상자를 클릭하면 표시됩니다.루프에서 jQuery에서 PHP 변수를 사용하려고합니다.

$("input[type='radio']").on('change', function() { 
    if ($(this).val() == "2") 
     var id = '<?php echo json_encode($absent); ?>'; 
     //$("input[type='text']").show(); 
    else 
     $("input[type='text']").hide(); 
}); 

PHP 코드 :

 <input name="nxtmark<?php echo $row[0]; ?>" type="text" id="nxtmark<?php echo $row['id']; ?>" onblur="onleave(this.name);" maxlength="2" placeholder="Enter Mark" hidden="" /> 
     <div class="noerrordiv" id="dnxtmark<?php echo $row[0]; ?>">Mark must not be blank</div> 
    </td> 
    <td> 
     <div align="justify"> 
      <input type="radio" id="rdopresent" name="rdopresent<?php echo $row['id']; ?>" checked="checked" value="1" />Present 
      <input type="radio" id="rdoabsent" name="rdopresent<?php echo $row['id']; ?>" value="2" />Absent<br /> 
     </div> 
    </td> 
</tr> 
+1

당신은 'var id = ...'를 버리고 그걸 할당하고 아무 것도하지 않기 때문에 버려야합니다. –

답변

0

당신은 PHP 변수를 사용하지 않고/숨기기 텍스트 상자를 표시 할 수 있습니다. 아래 코드 참조 -

$("input[type='radio']").on('change',function() { 
    //find input text from the previous 'td' for which name starts with 'nxtmark' 
    var $textBox = $(this).closest('td').prev('td').find('input[name^=nxtmark]'); 

    //show hide text box as per radio button value 
    if($(this).val() == "2") 
    $textBox.show(); 
    else 
    $textBox.hide(); 
}); 
0

문제가 jquery에서 PHP 변수를 사용하면 다음 예제가 도움이 될 것입니다.

jquery에서 PHP 배열을 사용자 지정해야하는 경우 PHP에서는 SCRIPT가 아닌 json_encode을 사용하십시오.

<?php 
    $a = array(1,2,3,4); 
    $a = json_encode($a); 
    $b = "hello"; 
?> 
<script> 
$(document).ready(function(){ 
    alert("<?php echo $a; ?>"); 
    alert("<?php echo $b; ?>"); 
}); 
</script> 
관련 문제