Intl tel input change dropdown dialcode from another dropdown

Intl tel input change dropdown dialcode from another dropdown

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

基于此示例 https://intl-tel-input.com/node_modules/intl-tel-input/examples/gen/country-sync.html,#phone 标志和#country 文本正在改变,反之亦然,但我希望拨号代码在 phone 号码字段上改变,也有时旗帜相互重叠,有什么帮助吗?谢谢

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);
});

没关系,我的错,调用了两次 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"
});