简体   繁体   English

使用 jquery [auto country by IP/intl-tel-input jquery library] 获取国家代码的输入值

[英]Get input value for country code with jquery [auto country by IP/intl-tel-input jquery library]

I need to submit COUNTRY CODE to URL based on the selected country value.我需要根据所选的国家/地区值将国家/地区代码提交给 URL。

Im using Intl-Tel-Input for this: https://github.com/jackocnr/intl-tel-input我为此使用 Intl-Tel-Input: https://github.com/jackocnr/intl-tel-input

Here is HTML code:这是 HTML 代码:

<form id="redirform" method="GET" action="#link"> 
                                <div class="gtd-form-wrapper">
                                    <input type="hidden" id="country" name="country">

                                <div class="form-group">
                                    <input name="phone" pattern="[0-9]*" id="phone" class="f w-input" data-value-missing="Use numbers only. No country code! No whitespaces!" required="required" placeholder="Phone">
                                </div> 

                                <div class="form-group">
                                    <button class="submit-button w-button gtd-form-submit" type="submit">SUBMIT</button>
                                </div>
                            </form> 

Here is jQuery:这是 jQuery:

        var input = document.querySelector("#phone");
        window.intlTelInput(input, {
            initialCountry: "auto",
            geoIpLookup: function(success, failure) {
                $.get("https://ipinfo.io/", function() {}, "jsonp").always(function(resp) {
                var countryCode = (resp && resp.country) ? resp.country : "";
                success(countryCode);
                });
            },
        });

        input.addEventListener('countrychange', function () {
                $('#country').val(iti.getSelectedCountryData().iso2);
            });

        $('#redirform').on('submit',function(){
        var $input = $(this).find("input[name=country]");
            $('#country').val(iti.getSelectedCountryData().iso2);
        });

Here is picture of what I have and what I need: PICTURE CLICK TO OPEN这是我拥有和需要的图片:图片点击打开

Seems you need to get the selected country value from the intl input.似乎您需要从 intl 输入中获取所选国家/地区的值。 You don't need to add country input in HTML.您不需要在 HTML 中添加国家/地区输入。

    var intl = window.intlTelInput(input, {
        initialCountry: "auto",
        geoIpLookup: function(success, failure) {
            $.get("https://ipinfo.io/", function() {}, "jsonp").always(function(resp) {
            var countryCode = (resp && resp.country) ? resp.country : "";
            success(countryCode);
            });
        },
    });

    $('#redirform').on('submit', function(e) {
        e.preventDefault();
        var countryData = iti.getSelectedCountryData();

        var form = $('#form');
        form.append($("<input>").attr("type", "hidden").attr("name", "country").val(countryData.iso2));

        form.submit();
    });

while the form is submitting, there is a hidden input is created and the value is set, so you don't need to worry about it.在表单提交时,会创建一个隐藏的输入并设置值,因此您无需担心。

Because you have told you to need to get the country value into URL, the form will submit as GET because the method of the form is GET.因为您告诉您需要将国家/地区值获取到 URL 中,所以表单将作为 GET 提交,因为表单的方法是 GET。

Hope this will help you.希望这会帮助你。 thanks.谢谢。

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

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