【问题标题】:Jquery International Telephone number inputjquery国际电话号码输入
【发布时间】:2022-01-06 18:49:41
【问题描述】:

我正在尝试按照 http://www.jqueryscript.net/form/jQuery-International-Telephone-Input-With-Flags-Dial-Codes.html 上的 jquery 教程在表单中创建带有国家代码的输入类型编号。但是,在我的情况下,虽然标志似乎发生了变化,但国家代码并未像演示中显示的那样添加到输入字段中。

以下是他们提供的代码:

<head>
<meta charset="utf-8">
<title>International Telephone Input</title>
<link rel="stylesheet" href="build/css/intlTelInput.css">
<link rel="stylesheet" href="build/css/demo.css">
</head>
   <body>

<form>
  <input id="mobile-number" type="tel">
  <button type="submit">Submit</button>
</form>

<!-- Load jQuery from CDN so can run demo immediately -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="build/js/intlTelInput.js"></script>
<script>
  $("#mobile-number").intlTelInput({
    //allowExtensions: true,
    //autoFormat: false,
    //autoHideDialCode: false,
    //autoPlaceholder: false,
    //defaultCountry: "auto",
    //ipinfoToken: "yolo",
    //nationalMode: false,
    //numberType: "MOBILE",
    //onlyCountries: ['us', 'gb', 'ch', 'ca', 'do'],
    //preferredCountries: ['cn', 'jp'],
    //preventInvalidNumbers: true,
    utilsScript: "lib/libphonenumber/build/utils.js"
  });
</script>

这是我尝试实现它的链接:https://www.easyaccom.com/mobile/verifynumber.html

我尝试更改脚本中注释掉的选项,但没有帮助。

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    好的,我解决了这个问题。 所以基本上这里是必须为真的选项字段,我们需要将下面的脚本放在&lt;/head&gt;标签之前。

    脚本:

    <script>
    $(function() {
    $("#mobile-number").intlTelInput({
    allowExtensions: true,
    autoFormat: false,
    autoHideDialCode: false,
    autoPlaceholder: false,
    defaultCountry: "auto",
    ipinfoToken: "yolo",
    nationalMode: false,
    numberType: "MOBILE",
    //onlyCountries: ['us', 'gb', 'ch', 'ca', 'do'],
    //preferredCountries: ['cn', 'jp'],
    preventInvalidNumbers: true,
    utilsScript: "lib/libphonenumber/build/utils.js"
    });
    });
    </script>
    

    将它放在关闭 head 标签之前,并记住调用$("#mobile-number").intlTelInput();,因为它很重要。

    【讨论】:

      【解决方案2】:

      您只是忘记将您的 jquery 代码放入 de document.ready 中。

      见下文:

      $(document).ready(function(){
      
          $("#mobile-number").intlTelInput({
              //allowExtensions: true,
              //autoFormat: false,
              //autoHideDialCode: false,
              //autoPlaceholder: false,
              //defaultCountry: "auto",
              //ipinfoToken: "yolo",
              //nationalMode: false,
              //numberType: "MOBILE",
              //onlyCountries: ['us', 'gb', 'ch', 'ca', 'do'],
              //preferredCountries: ['cn', 'jp'],
              //preventInvalidNumbers: true,
              utilsScript: "lib/libphonenumber/build/utils.js"
          });
      });
      

      【讨论】:

      • 不,这样做后我看不到标志
      • @AbhishekSingh 你说的是下拉菜单中的标志吗?
      • 是的,我说的是那个下拉菜单。添加文档就绪后消失
      • @AbhishekSingh 很奇怪,因为我尝试了您的网站及其工作,并在我使用正确的掩码更改值时更改
      • @AbhishekSingh 取消注释 nationalMode: false
      【解决方案3】:

      国家/地区代码将始终在此代码的输入字段中

      $("#mobile-number").on("blur keyup change", function() {
      if($(this).val() == '') {
          var getCode = $("#mobile-number").intlTelInput('getSelectedCountryData').dialCode;
          $(this).val('+'+getCode);
      }});
      $(document).on("click",".country",function(){
          if($("#phone").val() == '') {
              var getCode = $("#mobile-number").intlTelInput('getSelectedCountryData').dialCode;
              $("#mobile-number").val('+'+getCode);
      }});
      

      【讨论】:

        【解决方案4】:

        $(function() {
        $("#mobile-number").intlTelInput({
           autoHideDialCode: false,
           autoPlaceholder: false,
           nationalMode: false
        });
        });

        【讨论】:

          【解决方案5】:

          为什么国家标志和国家代码在页面回发时不稳定。

          这是我迄今为止尝试过的:

          <script>
                  $(document).ready(function () {
                      $("#Phone").intlTelInput({
                          allowDropdown: true,
                          // autoHideDialCode: false,
                          // autoPlaceholder: "off",
                          // dropdownContainer: "body",
                          // excludeCountries: ["us"],
                          defaultCountry: "auto",
                          // formatOnDisplay: false,
                          //geoIpLookup: function (callback) {
                          //    $.get("http://ipinfo.io", function () { }, "jsonp").always(function (resp) {
                          //        var countryCode = (resp && resp.country) ? resp.country : "";
                          //        callback(countryCode);
                          //    });
                          //},
                          //initialCountry: "auto",
                          // nationalMode: false,
                          // onlyCountries: ['us', 'gb', 'ch', 'ca', 'do'],
                          // placeholderNumberType: "MOBILE",
                          // preferredCountries: ['in','pk', 'np','bd', 'us','bt','sg','lk','ny','jp','hk','cn'],
                          // separateDialCode: true,
                          utilsScript: "build/js/utils.js"
                      });
          
                      $("#Phone").on("countrychange", function (e, countryData) {
                          $("#hdnPhone").val(countryData.dialCode);
                      });
                  });
            </script>
          <script>
                  $(document).ready(function () {
                      $("#Phone").val('');
                      var HdnVal = $("#hdnPhone").val();
          
                      if (HdnVal != '') {
                          var countryData = $("#Phone").intlTelInput("getSelectedCountryData");
          
                          $("#hdnPhone").val(countryData.dialCode);
                      }
                  });
          
              </script>
          

          【讨论】:

            【解决方案6】:
            try this code
            
            $("#mobile-number").intlTelInput({
                   // allowExtensions: true,
                   //autoFormat: false,
                    autoHideDialCode: false,
                   // autoPlaceholder: false,
                   // defaultCountry: "auto",
                    //ipinfoToken: "yolo",
                    nationalMode: false,
                   // numberType: "MOBILE",
                   // onlyCountries: ['us', 'gb', 'ch', 'ca', 'do'],
                    //preferredCountries: ['cn', 'jp'],
                    //preventInvalidNumbers: true,
                    utilsScript: "lib/libphonenumber/build/utils.js"
                });
            });
            

            【讨论】:

            • nationalMode: false, //numberType: "MOBILE",这两个是罪魁祸首lol
            【解决方案7】:
             //if you want to get coutry code value in input box then set  false 
                               nationalMode: false,
            
             separateDialCode: false,
            

            请在您将运行的函数属性中设置这两行

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-01-14
              • 1970-01-01
              • 2021-01-29
              • 1970-01-01
              相关资源
              最近更新 更多