简体   繁体   English

如何跨函数/变量访问javascript值?

[英]How do I access a javascript value across functions/variables?

So, three small parts: 因此,分为三个小部分:

1) a MaxMind geo IP lookup that gets us the country code via the IP address: 1)的MaxMind地理IP查找是通过IP地址得到我们国家代码:

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) an array of country references with tax percent decimals: 2)包含税号小数的国家/地区参考数组:

// 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) a TaxPrice function that takes one of those decimals to calculating tax and then total payable in a subscription form. 3)TaxPrice函数,该函数使用那些小数之一来计算税额,然后以订阅形式计算应付总额。 Notice the XXXXX: 注意XXXXX:

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

The document.getElementById bit in 1) can obviously update a hidden field or some other HTML element. 1)中的document.getElementById位显然可以更新隐藏字段或其他HTML元素。

I know what to do with XXXXX if it's a manual drop down the user has to select. 我知道该怎么做XXXXX如果是手动下拉的用户选择。

But how do I get the tax decimal out of the array and into the TaxPrice function based on the IP address country code? 但是,如何根据IP地址国家/地区代码从数组中获取税小数并进入TaxPrice函数? (ie within the javascript, not updating an HTML element). (即在javascript中,而不是更新HTML元素)。

Happy New Year to all. 祝大家新年快乐。

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. 更新:需要明确的是,我不需要知道如何将其放入下拉列表中,我已经可以做到这一点,在这种情况下,不应允许用户选择自己的纳税国家/地区,根据IP地址自动设置。 So the non-code wording would go something like: 因此,非代码措辞将类似于:

taxprice EQUALS tax_rate.value ACCORDING TO ip_address_code

Are you looking for something like element properties? 您是否正在寻找元素属性之类的东西? Mydiv.tax=taxvalue; Mydiv.tax =税值; Properties of Elements are an elegant way of communicating between different functions. 元素的属性是在不同功能之间进行通信的一种优雅方式。 You can assign any value to any element. 您可以将任何值分配给任何元素。 You can retrieve the value from any function in JavaScript as long as the Basic element lives. 只要Basic元素存在,就可以从JavaScript中的任何函数中检索值。

One way you can do it is to set a global selectedCountryCode variable inside your success callback, and reference tax_rates[selectedCountryCode] in your TaxPrice array (which should be an object, as nnnnnn pointed out) 一种实现方法是在成功回调中设置全局的selectedCountryCode变量,并在TaxPrice数组中引用tax_rates[selectedCountryCode] (应该是一个对象,如nnnnnn所指出的)

 (function () { var selectedCountryCode = ""; var onSuccess = function(x) { var ip = x.traits.ip_address; document.getElementById('ip_address').value = ip; selectedCountryCode = x.country.iso_code; // <-- Set selectedCountryCode for later use document.getElementById('ip_country_code').value = selectedCountryCode; // <-- Set dropdown value }; document.getElementById("ip_country_code").addEventListener("change", function() { selectedCountryCode = this.value; console.log(TaxPrice()); }); // Object of values for tax rates var tax_rates = {}; tax_rates["noteu"] = 0.0; tax_rates["ES"] = 21.0; tax_rates["AU"] = 20.5; tax_rates["BE"] = 21.7; function TaxPrice() { var taxprice = 0; taxprice = tax_rates[selectedCountryCode]; return taxprice; } })(); 
 Change Me: <select id="ip_country_code"> <option value="noteu">noteu</option> <option value="ES">ES</option> <option value="AU">AU</option> <option value="BE">BE</option> </select> 

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) Add a global variable at the top, above everything else, unrelated to the IP lookup code (ie there is now no reference to country_tax within the IP onSuccess variable): 1)在其他所有内容的最上方添加一个与IP查找代码无关的全局变量(即IP onSuccess变量中现在没有对country_tax引用):

var country_tax;

2) Replace XXXXX in the TaxPrice function with: 2)将TaxPrice函数中的XXXXX替换为:

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

So the full TaxPrice function ends up as: 因此,完整的TaxPrice函数最终结果为:

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

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. (对于代码而言)将tax_rates设置为数组还是对象都没有关系,结果是相同的,尽管我想理解为什么在这种情况下为什么建议在数组上推荐对象。

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… 而且-给定TaxPrice是从表单字段而不是从IP onSuccess函数内部获取值的-我不知道为什么我需要顶部的全局变量声明,如果有人想去解释一下……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM