最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - On change event for country code in intlTelInput - Stack Overflow

programmeradmin1浏览0评论

I am trying to set onchange event in intlTelInput (jQuery). But it's not working . If I change country code then it will listen as change . Here is my code. It's working when I change mobile number. But I want it when I change country code. Thanks in advance.

<input type="text" name="user_phone_number" id="user_phone_number"> 

$('input[name=user_phone_number]').change(function() {
     var countryCode = $("#user_phone_number").intlTelInput("getSelectedCountryData").dialCode;
     alert(countryCode);
            
 });

I am trying to set onchange event in intlTelInput (jQuery). But it's not working . If I change country code then it will listen as change . Here is my code. It's working when I change mobile number. But I want it when I change country code. Thanks in advance.

<input type="text" name="user_phone_number" id="user_phone_number"> 

$('input[name=user_phone_number]').change(function() {
     var countryCode = $("#user_phone_number").intlTelInput("getSelectedCountryData").dialCode;
     alert(countryCode);
            
 });
Share Improve this question asked Jul 22, 2020 at 7:05 MithunMithun 2651 gold badge10 silver badges19 bronze badges 1
  • Please define "it's not working". – Teemu Commented Jul 22, 2020 at 7:12
Add a ment  | 

3 Answers 3

Reset to default 2

check the documentation, you can use

input.addEventListener("countrychange", function() {
  // do something with iti.getSelectedCountryData()
});

and try this solution also

var input = $("#user_phone_number");
input.intlTelInput();

input.on("countrychange", function() {
  input.val('')
});

For those who want to change selection by country ID

$('#user_phone_number').intlTelInput("setCountry", "us");

try this, worked for me:

$("#ElementId").on("countrychange", function () {
                var SelectedCountry = $("#ElementId").intlTelInput("getSelectedCountryData");
                console.log(SelectedCountry );
           });
发布评论

评论列表(0)

  1. 暂无评论