如何从国际电话输入中获取国家价值?

How to get country value from intl-tel-input?

所以我是 javascript 的新手。我想创建有两个字段的表单,phone 数字和国家/地区。我想在我的数据库中将它们分开。我想知道如何从 intl-tel-input ( https://github.com/jackocnr/intl-tel-input ) 中获取国家/地区值。 例如 HTML

                      <form>
                        <label>Phone Number</label>
                        <!-- using intl-tel-input -->
                        <input type="tel" name="phone">

                        <label>Country</label>
                        <!-- how can i get country value automatically here from phone number field? -->
                        <input type="text" name="country">
                      </form>

谁能帮我解决这个问题?提前致谢。

<!-- REQUIRED CDN  -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.8/js/intlTelInput.min.js"
crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.8/css/intlTelInput.min.css"
crossorigin="anonymous" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.8/js/utils.js" crossorigin="anonymous"></script>

<input name="phone" type="text" class="form-control mb-2 inptFielsd" id="phone"
placeholder="Phone Number" />

<label>Country</label>
<input id="country" type="text" name="country">

<script>
    var input = document.querySelector("#phone");
    window.intlTelInput(input, {
        separateDialCode: true,
        customPlaceholder: function (
            selectedCountryPlaceholder,
            selectedCountryData
        ) {
            return "e.g. " + selectedCountryPlaceholder;
        },
    });
  
    var iti = window.intlTelInputGlobals.getInstance(input);

    input.addEventListener('input', function() { 
      var countryName = iti.getSelectedCountryData().name;
      document.getElementById('country').value = countryName;
    });
</script>