如何跨函数/变量访问javascript值? [英] How do I access a javascript value across functions/variables?

查看:100
本文介绍了如何跨函数/变量访问javascript值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,分为三个小部分:

So, three small parts:

1)MaxMind地理IP查找,可通过IP地址为我们获取国家/地区代码:

1) a MaxMind geo IP lookup that gets us the country code via the IP address:

var onSuccess = function(x){

var ip = x.traits.ip_address;
document.getElementById('ip_address').value = ip;

var country_code = x.country.iso_code;
document.getElementById('ip_country_code').value = country_code;

…

};

2)包含税号小数的国家/地区参考数组:

2) an array of country references with tax percent decimals:

// Array of values for tax rates
var tax_rates= new Array();
tax_rates["noteu"]=0.0;
tax_rates["ES"]=21.0;
tax_rates["AU"]=20.5;
tax_rates["BE"]=21.7;
…

3)TaxPrice函数,该函数使用那些小数之一来计算税款,然后以订阅形式计算应付总额.注意XXXXX:

3) a TaxPrice function that takes one of those decimals to calculating tax and then total payable in a subscription form. Notice the XXXXX:

function TaxPrice()
{
var taxprice=0;
XXXXX
return taxprice;
}

1)中的document.getElementById位显然可以更新隐藏字段或其他HTML元素.

The document.getElementById bit in 1) can obviously update a hidden field or some other HTML element.

如果用户必须手动选择下拉菜单,我知道该如何处理XXXXX.

I know what to do with XXXXX if it's a manual drop down the user has to select.

但是如何根据IP地址国家/地区代码从数组中获取税小数并进入TaxPrice函数? (即在javascript中,而不是更新HTML元素).

But how do I get the tax decimal out of the array and into the TaxPrice function based on the IP address country code? (i.e. within the javascript, not updating an HTML element).

祝大家新年快乐.

更新:为清楚起见,我不需要知道如何将其放入下拉列表,我已经可以做到这一点,在这种使用情况下,不应允许用户选择自己的纳税国家/地区,它应该根据IP地址自动设置.因此,非代码措辞将类似于:

UPDATE: Just to be clear, I don't need to know how to get it into a drop down, I can do that already and in this use case, the user should not be allowed to choose his own tax country, it should be set automatically based on the IP address. So the non-code wording would go something like:

taxprice EQUALS tax_rate.value ACCORDING TO ip_address_code

推荐答案

因此,感谢您的建议.不知道我是否理解它是如何工作的,但是经过一番摸索,现在可以了.与原始问题中的代码相比,我不得不:

So, thanks for your suggestions. Not sure if I understand how this is working exactly, but after some poking around, it now is. Compared to the code in the original question, I had to:

1)在所有其他内容的最上方添加一个与IP查找代码无关的全局变量(即IP onSuccess变量中现在没有对country_tax的引用):

1) Add a global variable at the top, above everything else, unrelated to the IP lookup code (i.e. there is now no reference to country_tax within the IP onSuccess variable):

var country_tax;

2)将TaxPrice函数中的XXXXX替换为:

2) Replace XXXXX in the TaxPrice function with:

var country_tax = document.getElementById("ip_country_code").value;
var taxprice = taxes_from_database[country_tax];

因此完整的TaxPrice功能最终显示为:

So the full TaxPrice function ends up as:

function TaxPrice() 
{
var taxprice = 0;
var country_tax = document.getElementById("ip_country_code").value;
var taxprice = tax_rates[country_tax];
return taxprice;
}

似乎不需要嵌套函数或闭包或任何非常复杂的东西. (对于代码而言)将tax_rates设置为数组还是对象都没有关系,尽管我想理解为什么在这种情况下为什么建议在数组上推荐对象,但是结果是相同的.

No need, it seems, for nested functions or closures or anything very complicated. It doesn't matter (to the code) if the tax_rates are set up as an array or an object, the outcome is the same, although I would like to understand why you recommend an object over an array in this case.

并且-鉴于TaxPrice从表单字段而不是从IP onSuccess函数内部获取值-我不知道为什么我需要在顶部使用全局变量声明,如果有人想去解释一下……

And—given TaxPrice gets the value from the form field and not from within the IP onSuccess function—I don't know why I need the global variable declaration at the top, if anyone wants to have a go at explaining that…

这篇关于如何跨函数/变量访问javascript值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆