简体   繁体   English

通过ISO代码提交国家/地区-html表单

[英]submit country by ISO code - html form

So i would like to offer a list of all countries for a user to select from a form. 因此,我想提供所有国家/地区的列表,供用户从表格中选择。 I found a pretty good list of countries in html format from this site. 我从该站点以html格式找到了相当不错的国家/地区列表 The site implements my task by listing out the full country name as an html string but the form actually sends its ISO code to the server. 该站点通过将完整的国家/地区名称列出为html字符串来实现我的任务,但是该表单实际上将其ISO代码发送到服务器。 This is great but how would someone tackle the issue of presenting the full country name when reading the submitted values? 很好,但是当阅读提交的值时,有人将如何解决提供国家全名的问题?

Do you think its best to submit a country value as ISO values or should I just submit the full name name of the country? 您认为最好将国家/地区值作为ISO值提交,还是我应该只提交国家/地区的全名? All sites seem to be doing the earlier option but it just seems like a hassle to me. 所有站点似乎都在使用较早的选项,但是对我来说似乎很麻烦。 Can someone present some thoughts to this matter explaining why to use ISO codes in the first place and how to decode them back to country names if their ISO were used? 有人可以对此事发表一些想法,解释为什么首先使用ISO代码,以及如果使用ISO则如何将其解码回国家名称? thank you 谢谢

As per my comment... The idea is, when the select changes, it calls the JS function. 根据我的评论...的想法是,当选择更改时,它将调用JS函数。 In there, you have an array set up with the key matching the value of the select, and what you want it to show. 在其中,您建立了一个数组,其中的键与select的值以及要显示的内容匹配。 So, you'd just need to pass that code the array to get the value. 因此,您只需要将该代码传递给数组即可获取值。

<form>

<select onchange="showName()" id="country">
    <option value="AF">Afghanistan</option>
    <option value="AX">Åland Islands</option>
    <option value="AL">Albania</option>
    <option value="DZ">Algeria</option>
    <option value="AS">American Samoa</option>
    <option value="AD">Andorra</option>
    <option value="AO">Angola</option>
    <option value="AI">Anguilla</option>
    <option value="AQ">Antarctica</option>
    <option value="AG">Antigua and Barbuda</option>
    <option value="AR">Argentina</option>
    <option value="AM">Armenia</option>
    <option value="AW">Aruba</option>
    <option value="AU">Australia</option>
    <option value="AT">Austria</option>
    <option value="AZ">Azerbaijan</option>
    <option value="BS">Bahamas</option>
...
</select>
<div id="selResult">
selected: nothing
</div>
</form>

JS: JS:

var myArray = {"AF": "Afghanistan", "AX": "Åland Islands", "AL": "Albania"};
function showName(){
  selectRes = myArray[document.getElementById("country").value];
  document.getElementById("selResult").innerHTML = selectRes;
}

You'll have to create the complete list. 您必须创建完整的列表。 But that's the basic idea how to make it work. 但这是如何使其工作的基本思想。

This here fiddle has this whole thing, plus one additional way to make use of the reference array. 这里的小提琴具有全部内容,外加一种使用引用数组的其他方法。

For what it's worth, if you intend to use this a lot, you should consider creating a reference table in your database, rather than mucking around in JS to do this. 对于它的价值,如果您打算经常使用它,则应考虑在数据库中创建一个引用表,而不是在JS中随意处理。

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

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