来自另一个下拉菜单的国际电话输入更改下拉菜单拨号代码

时间:2019-08-08 06:09:29

标签: javascript intl-tel-input

尝试使用国际电话输入插件解决问题。

基于此示例https://intl-tel-input.com/node_modules/intl-tel-input/examples/gen/country-sync.html,#phone标志和#country文本反过来发生了变化,但是我希望更改电话号码字段上的拨号代码,有时这些标志彼此重叠,有帮助吗?谢谢

var countryData = window.intlTelInputGlobals.getCountryData(),
input = document.querySelector("#phone"),
countryDropdown = document.querySelector("#country");

intlTelInput(input, {
    allowDropdown: true,
    autoHideDialCode: false,
    autoPlaceholder: "off",
    dropdownContainer: document.body,
    preferredCountries: ['jp'],
    separateDialCode: true,
    customContainer: "col-md-12 no-padding intelinput-styles",
    //utilsScript: "assets/js/utils.js"
});


var iti = intlTelInput(input, {
    utilsScript: "./utils.js" 
});

for (var i = 0; i < countryData.length; i++) {
    var country = countryData[i];
    var optionNode = document.createElement("option");
    optionNode.value = country.iso2;
    var textNode = document.createTextNode(country.name);
    optionNode.appendChild(textNode);
    countryDropdown.appendChild(optionNode);
}


input.addEventListener('countrychange', function(e) {
    countryDropdown.value = iti.getSelectedCountryData().iso2;
});

countryDropdown.addEventListener('change', function() {
    iti.setCountry(this.value);
});

1 个答案:

答案 0 :(得分:0)

没关系,我的坏人叫了两次init。

此处初始化:

 var iti = window.intlTelInput(input, {
    utilsScript: "assets/js/utils.js",
    initialCountry: "jp",   
    separateDialCode: true,
    customContainer: "col-md-12 no-padding intelinput-styles",
});

删除此内容:

intlTelInput(input, {
    allowDropdown: true,
    autoHideDialCode: false,
    autoPlaceholder: "off",
    dropdownContainer: document.body,
    preferredCountries: ['jp'],
    separateDialCode: true,
    customContainer: "col-md-12 no-padding intelinput-styles",
    //utilsScript: "assets/js/utils.js"
});