2012-11-16 5 views
0

에서 변수를 나가 (나는 그것을 기록하지 않았다).나는이 기능이 자바 스크립트 함수

<script>document.write(field_number);</script> 

가 어떻게 그것을 얻을 수 있습니다 : 나는 함수의 외부 field_number의 값을 추출 할 필요가 HTML의 다른 부분에 작동하게하는? 감사합니다. .

+0

는 따라 다릅니다. 'ab'는 무엇을합니까? 'ab'는 수정할 수있는 함수입니까? 콜백에서'field_number'를 사용하지 않는 이유가 있습니까? –

+0

수정할 수 없습니다. 나는 field_number를 얻을 수있다. (만약 내가 어떻게 알았다면!) –

+0

'ab'는 무엇을 하는가? 콜백에서'field_number'를 사용하지 않는 이유가 있습니까? –

답변

0

당신이 전역 변수를 설정할 수 있습니다 ab()에 대한 비동기 호출이없는 경우 :

<script> 
     var field_number=""; 
     ab(function(r) { 
     field_number = r.get('field_number'); 
     alert(field_number); 
     }); 
     // you can use variable here 
    </script> 

을 아니면 함수에서 반환하고 사용 후 변수에 할당 할 수있는가. 할

+0

이것은 작동하지 않습니다 :'var field_number; ab (function (r) {field_number = r.get ('field_number'); alert (field_number);}); document.write (field_number);' "Undefined"를 쓰고 있지만 경고가 올바른 값을 출력합니다. –

+0

document.write() 대신에 함수 밖에서 경고가 발생합니다. –

+0

콜백이있는 경우 대부분 'ab'가 비동기 코드를 수행하므로 변수는 여전히'ab' 호출 이후에 '정의되지 않음'이됩니다. –

0

가장 간단하고 가장 좋은 방법은 다음과 같습니다

<script> 

var a=""; //global variable 

    function process() 
    { 
     a=1; 
    process_another(a) //another function in which you want the value of a 

    } 

    function process_another(a) 
    { 
     alert(a); //value of a will be shown: 1 as it was in function process() 
     } 
      </script> 
관련 문제