2013-02-12 4 views
0

jquery를 처음 사용했습니다. 내 현재 CakePHP의 페이지 URL에 있습니다동일한 PHP의 json 배열에서 jQuery를 사용하여 일치하는 데이터 가져 오기

http://localhost/ultimate/admin/treatments/add 

내가 JSON 배열을 가지고 : <?php $json = $this->Js->object($matchs);?>를 내용이있다 : 이제 '[{"Type":{"name":"Items 1","price":"15","duration":"10"}},{"Type":{"name":"Items 2","price":"30","duration":"25"}}]'

나는 이름이 : 항목 1, 내가 JSON 배열에서 가격을 얻을 수있는 방법 Jquery를 사용하고 하나의 입력 영역을 업데이트합니다.

이런 식으로 뭔가,하지만 난 아무 생각이 :

$(document).ready(function(){ 
    $('#treatment_foo').change(function(){ 

     $.ajax({ 
      url: 'add', 
      data: 
      dataType: 'json', 
      cache: false, 
      success: function(result) { 
     $('#fee_foo').val($('#treatment_foo').val()); 
     }, 

     }); 
    }); 
}); 
+0

AJAX 호출에 대한 응답으로 PHP에서이 배열을 반환한다고 가정합니다. –

+0

예, cakephp에서 배열을 가져 와서 아약스에서 사용하고 싶습니다. – Victor

답변

0
success: function(result) { 
    $('#fee_foo').val(result[0].Type.price); 
}, 
+0

고마워요,하지만 json에서 'item 1'과 어떻게 일치합니까? – Victor

0

또한 getJson jQuery를 기능을 사용할 수 있습니다.


...이 결과 json 표시 통해 루프 할 수있는보고 당신이 필요로 비교,
건배 수 있듯이

<script type="text/javascript" src="js/jquery.js"></script> 
<script type="text/javascript"> 

    (function($) 
    { 
     $.getJSON(URL, 
     { 
      params1: "value", 
      params2: "value" 
     }, 
     function(result) 
     { 
      $.each(result.Type, function(i,item) 
      { 
       console.log(item.name); 
       console.log(item.price); 
       console.log(item.duration); 
       if (item.name == 'Item 1') return; 
      }); 
     }); 
    }); 

</script>

처럼 물어 주시기 바랍니다.

관련 문제