Using intl-tel-input and vuejs2 together

怎甘沉沦 提交于 2019-12-23 12:06:14

问题


I am trying to implement https://github.com/jackocnr/intl-tel-input with vuejs2.

If I add inside one jQuerydocument.ready, $('#phone').intlTelInput({ options...})

Everything works as expected but when I'm trying to fetch the value of the input field and some boolean valid property, I am always checking the value one step behind.

My checking method looks like:

    validatePhoneNumber: function() {
        var validPhoneNo = $("#phone").intlTelInput("isValidNumber");
        var phoneNo = $("#phone").intlTelInput("getNumber");
        console.log(phoneNo + ' -> ' + validPhoneNo); //this line returns values one step behind
        bus.$emit('validate-phone', validPhoneNo)
    }

The HTML:

<input class="form-control" @keydown="validatePhoneNumber" :class="{validInput: validPhone }" type="text" name="phone" value="" id="phone" :phone_number="phone_number">

I believe the solution would be to create a new directive for this matter, and I tried the following:

Removed the part where I instantiated the intlTelInput on document.ready and made this instead:

Vue.directive('telinput', {
    bind: function(el) {
        $(el).intlTelInput({
            //options here...
    }
});

and added v-telinput to my HTML code above.

with this, nothing seems to work.

What am I missing there?


回答1:


Try to use @keyup instead of @keydown



来源:https://stackoverflow.com/questions/42792983/using-intl-tel-input-and-vuejs2-together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!