簡體   English   中英

intlTelInput 的 jquery 無法添加事件偵聽器

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

嘗試執行類似於國家/地區代碼偵聽器下拉列表的操作,但帶有一個文本框,該文本框應填充國家/地區代碼(以便我隨后可以在 PHP 代碼中使用該值)。

添加事件偵聽器不起作用:

 <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>

也許,你問這個問題已經幾個月了,下面是解決方案。

您誤解了addEventListener 這可以是任何其他 Javascript 框架的功能。 但不是 jQuery。

初始化后使用以下代碼作為腳本。 您可以使用countryData.dialCode為您提供國家/地區代碼。

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

下面是intlTelInput返回的示例countryData

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

我的工作版本是(這是基於 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