简体   繁体   English

intlTelInput 的 jquery 无法添加事件侦听器

[英]jquery for intlTelInput can't add event listener

trying to do something similar to the country code listener drop down but with a text box which should populate with the country code (so that I can subsequently use the value in PHP code).尝试执行类似于国家/地区代码侦听器下拉列表的操作,但带有一个文本框,该文本框应填充国家/地区代码(以便我随后可以在 PHP 代码中使用该值)。

Adding the event listener doesn't work:添加事件侦听器不起作用:

 <script> $(document).ready(function(){ $("#mobile").intlTelInput({ onlyCountries: ["au","ca","us","nz","gb"], utilsScript: "<?php echo get_template_directory_uri(); ?>/intl-tel-input/build/js/utils.js", geoIpLookup: function(callback) { $.get("https://ipinfo.io", function() {}, "jsonp").always(function(resp) { var countryCode = (resp && resp.country) ? resp.country : ""; callback(countryCode); }); }, initialCountry: "auto" }).addEventListener('countrychange', function() { $("#country_code").value = "changed" });; $('#verify').attr('disabled','disabled'); $("#mobile").on('input', function() { if ($("#mobile").intlTelInput("isValidNumber")) { $('#verify').removeAttr('disabled'); $("#country_code").innerHTML = "changed" } else { $('#verify').attr('disabled','disabled'); } }); $("#register-form").submit(function() { $("#mobile").val($("#mobile").intlTelInput("getNumber")); }); }); </script>

Maybe, it has been months since you asked this question, below is the solution.也许,你问这个问题已经几个月了,下面是解决方案。

You misunderstood the addEventListener .您误解了addEventListener This could be a function of any other Javascript framework.这可以是任何其他 Javascript 框架的功能。 But not jQuery's.但不是 jQuery。

User the below code as a script after the initialization.初始化后使用以下代码作为脚本。 You can do what your you want with countryData.dialCode which gives you the country code.您可以使用countryData.dialCode为您提供国家/地区代码。

jQuery("#mobile").on('countrychange', function(e, countryData){
    console.log(countryData.dialCode);
})

Below is the sample countryData which the intlTelInput returns.下面是intlTelInput返回的示例countryData

{ areaCodes: null, dialCode: "44", iso2: "gb", name: "United Kingdom", priority: 0 }

My working version is (this is based on with jQuery library version of Intelinput)我的工作版本是(这是基于 Intelinput 的 jQuery 库版本)

var mobile_with_country_code = '<?php echo $mobile_with_country_code; ?>';
        
        var mobile_number_input = document.querySelector("#mobile");

        mobile_number = window.intlTelInput(mobile_number_input, {
            initialCountry: "ae",
            separateDialCode: true,
            preferredCountries: ["ae","bh","kw","om","qa","sa"],
        });

        if(mobile_with_country_code != ''){
            mobile_number.setNumber(mobile_with_country_code);
        }

        jQuery('#country_code').val(mobile_number.getSelectedCountryData().dialCode);

        mobile_number_input.addEventListener("countrychange", function() {
          jQuery('#country_code').val(mobile_number.getSelectedCountryData().dialCode);
        });

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

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