다음 AJAX가 있습니다.jQuery에서 변수의 riptscope
내가해야 할 일은 외부의 $.ajax
호출을 사용하는 것입니다. 변수 tax_rate
이 [1]에 적절한 값을 보여주기 때문에 내 AJAX 호출이 제대로 작동하지만 [2]에서는 변수가 쓸모 없게됩니다.
가변 범위에 문제가 있습니까?
<script>
jQuery(document).ready(function($) {
$(document).on('blur','#form',function() {
var subsum = 0;
var tax_rate = 0;
var tax_amount = 0;
$('.form-row').each(function() {
// calculate sums and totals per each form-row
subsum = Number($(this).find('.unit').val() * $(this).find('.price').val()).toFixed(2);
$(this).find('.subsum').empty().val(subsum);
// calculate tax amount
var tax_id= $(this).find('.tax_code').val();
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
// call ajax to retrieve the tax rate
$.ajax({
url: ajaxurl,
data:' action=get_tax_rate_by_id&tax_id='+tax_id,
type:'GET',
success:function(results) {
tax_rate = results;
alert(tax_rate); // [1] Obviously, this one properly alerts tax rate.
}
});
alert(tax_rate); // [2] This one always alerts 0.
});
});
});
_AJAX_는 __Asynchronous__를 나타내며, _ 존재하지 않거나 동시에 발생 함을 의미합니다 ._. – Satpal