gpt4 book ai didi

javascript - 如何使用基于 IP 的国家下拉列表显示国家名称、国旗和国家代码

转载 作者:行者123 更新时间:2023-12-04 17:16:19 29 4
gpt4 key购买 nike

我正在尝试生成电话输入字段,我已经成功了。

要求 1:需要在下拉列表中显示国旗和国家名称要求 2:一旦选择了国家/地区,它将在下一个文本字段中显示国家/地区代码。

输出是这样的:

enter image description here

$("#mobile-number").intlTelInput({
initialCountry: "auto",
geoIpLookup: function(callback) {
$.get('https://ipinfo.io', function() {}, "jsonp").always(function(resp) {
var countryCode = (resp && resp.country) ? resp.country : "";
callback(countryCode);
});
},
utilsScript: "https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/11.0.9/js/utils.js" // just for formatting/placeholders etc
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://www.jquery-az.com/jquery/js/intlTelInput/intlTelInput.js"></script>
<link href="https://www.jquery-az.com/jquery/css/intlTelInput/demo.css" rel="stylesheet" />
<link href="https://www.jquery-az.com/jquery/css/intlTelInput/intlTelInput.css" rel="stylesheet" />

<h1>A demo with IP lookup</h1>
<form style="margin-left:150px;">
<input type="tel" id="mobile-number" placeholder="e.g. +1 702 123 4567">
</form>

最佳答案

我尝试使用 jQuery。

$("#mobile-number").intlTelInput({
initialCountry: "auto",
geoIpLookup: function(callback) {
$.get('https://ipinfo.io', function() {}, "jsonp").always(function(resp) {
var countryCode = (resp && resp.country) ? resp.country : "";
callback(countryCode);
init();
});
},
utilsScript: "https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/11.0.9/js/utils.js" // just for formatting/placeholders etc
});
function init() {
$(".selected-flag").css("width", "150px");
$(".selected-flag").append($("<div id='country-name'>"+name+"</div>").css({
"position": "absolute",
"top": "0",
"bottom": "0",
"right": "0",
"width": "100",
"display": "flex",
"align-items": "center",
"overflow": "hidden",
"white-space": "nowrap"
}));
$("#mobile-number").css({"padding-left":"150px", "width":"300px"});
showCountryAndCode();
};
$(".country-list li").click(function() {
showCountryAndCode();
});
function showCountryAndCode() {
setTimeout(() => {
let name = $("li.active span.country-name").text();
name = name.indexOf('(') > 0 ? name.substr(0, name.indexOf('(') - 1) : name;
const code = $("li.active span.dial-code").text();
$("#country-name").text(name);
$("#mobile-number").val(code+" ");
}, 0);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://www.jquery-az.com/jquery/js/intlTelInput/intlTelInput.js"></script>
<link href="https://www.jquery-az.com/jquery/css/intlTelInput/demo.css" rel="stylesheet" />
<link href="https://www.jquery-az.com/jquery/css/intlTelInput/intlTelInput.css" rel="stylesheet" />

<h1>A demo with IP lookup</h1>
<form style="margin-left:150px;">
<input type="tel" id="mobile-number" placeholder="e.g. +1 702 123 4567">
</form>

希望对你有帮助。

关于javascript - 如何使用基于 IP 的国家下拉列表显示国家名称、国旗和国家代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68652667/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com