2015-01-28 2 views
0

안녕하세요 저는 javascript에 익숙하지 않습니다. 테이블에서 여러 값을 반환해야합니다. 현재 jscript는 테이블의 첫 번째 값만 반환합니다.자바 스크립트에서 여러 값을 반환하는 방법

내 자바 스크립트

<script> 
jQuery(document).ready(function($){ 
    $("#countryDropDown").change(function() 
    { 
     var country=$(this).val(); 
     function fillStateDropDown(countryStateOption) 
     { 
      $.each(countryStateOption, function(val, text) { 
       $('#stateDropDown').append(
       $('<option></option>').val(val).html(text)); 
      }); 
     } 
     function clearStateDropDown() 
     { 
      $('#stateDropDown option:gt(0)').remove(); 
     } 
     if(country=='Art1') 
     { 
      clearStateDropDown(); 
      var indiaStateOption={ 
       '<?=$row['service']?>' : '<?=$row['service']?>', 
      }; 
      fillStateDropDown(indiaStateOption); 
     } 

PHP 코드

<?php 
    $select=mysql_query("select * from service where status=1") or die(mysql_error()); 
    while($row=mysql_fetch_array($select)){ 
?> 

값 | b | v | v |

현재 첫 번째 값 A를 반환하지만 모든 값을 인쇄해야합니다. 감사합니다

+2

자바 스크립트는하지만 그 값은, 내가 '돈의 모든 결과 – atmd

+0

정말를 포함하는 배열 될 수있다, 당신은 1 개 값을 반환 할 수 있습니다 함수에서 하나 이상의 값을 반환 할 수있는 프로그래밍 언어를 알아야합니다. – degr

+0

PHP와 JS의 혼합을 살펴보면 질문이 정확한지조차 알지 못합니다 ... 코드가 들여 쓰기가 더 안 좋게 보입니다.그가 실제로 PHP에 대해 물어볼 가능성이 있습니까 ??? –

답변

0

PHP 쿼리 결과에 따라 select html 요소에서 옵션을 출력하려고합니까? 당신은, 첫째로 여기

내가 쿼리가 여러 행을 반환 가정 http://jsfiddle.net/Lj4rqkpy/

<body> 
<select id="countryDropDown"> 
<option value="Art1">this shows first</option> 
<option value="Art1">select me to trigger on change</option> 
</select> 

<select id="stateDropDown"> 
<option value="">This will be cleared</option> 
</select> 
</body> 

jQuery(document).ready(function($) 
{ 
    $("#countryDropDown").change(function() 
    { 
     var country = $(this).val(); 

     function fillStateDropDown(countryStateOption) 
     { 
      $.each(countryStateOption, function(val, text) 
      { 
       $('#stateDropDown').append(
        $('<option></option>').val(val).html(text) 
       ); 
      }); 
     } 
     function clearStateDropDown() 
     { 
      $('#stateDropDown').html(''); 
     } 

     if(country == 'Art1') 
     { 
      clearStateDropDown(); 
      var indiaStateOption = { 
       'testService' : 'testService', 'testService2' : 'testService2' 
      }; 
      fillStateDropDown(indiaStateOption); 
     } 
    }); 

    // 
}); 

을하려고 생각의 거친 모형, 그리고 당신은 그들이 당신의 자바 스크립트에 indiaStateOption 객체의 내부 출력 할 JS에서

<?php 
    $serviceJSOutput = ''; 
    $select=mysql_query("select * from service where status=1") or die(mysql_error()); 
    while($row=mysql_fetch_array($select)){ 
     $serviceJSOutput .= "'" . $row['service'] . "' : '" . $row['service'] . "',"; 
    } 
    // remove the last comma 
    $serviceJSOutput = rtrim($serviceJSOutput, ","); 
?> 

다음을 사용할 수있는 것은

if(country == 'Art1') 
{ 
    clearStateDropDown(); 
    var indiaStateOption = { 
     <?=$serviceJSOutput?> 
    }; 
    fillStateDropDown(indiaStateOption); 
} 
로 if 문을 대체

제쳐두고 나는 가능하면 PHP에서 mysql_query를 사용하지 말 것을 권한다. 감가 상각된다. PDO 또는 mysqli를 사용하십시오.

내 대답은 가정으로 채워져 있으므로 완전히 테스트 할 시간이 없었으므로 잘못 입력 한 부분을 수정하십시오.

+0

이 질문에 대한 답을 제공하지 않습니다. 비평하거나 저자의 설명을 요청하려면 게시물 아래에 의견을 남기십시오. 자신의 게시물에 언제나 댓글을 달 수 있으며 충분한 [평판] (http://stackoverflow.com/help/whats-reputation)을 갖게되면 [모든 게시물에 댓글을 달 수] 있어야합니다 (http://stackoverflow.com/help/privileges/comment). –

+0

나는 코드를 읽고 그들이 필요한 것을 더 잘 이해하려고 노력했다. 틀림없이 나는 표식이 될 수도 있지만 이전의 대답보다 훨씬 낫다고 생각합니다. –

0

JSON 객체를 사용하면 자바 스크립트에서 여러 객체를 쉽게 반환 할 수 있습니다. .js 스크립트 파일에서 JSON 객체를 반환하고 jquery를 사용하여 javascript에서 해당 값을 검색합니다.

0

개체를 반환하십시오. 이 같은

var result = {}; 
result.a=20; 
result.x='ABC'; 
return result; 

다음 반환에 액세스 할 수 있습니다 개체 속성 :

alert(result.x); 
alert(result.a); 
0

당신은 쉽게 배열 또는 명명 된 키를 사용하여 객체를 반환 할 수 있습니다, 자바 스크립트의 인라인 생성 구문은 매우 쉽습니다.

배열 예 :

return [value1, value2, finalValue]; 

개체 예 :

return { 
    value1Name: value1, 
    value2Name: value2, 
    finalValueName: finalValue 
} 
관련 문제