Laravel 5.6:App.js的国际电话输入兼容性问题

时间:2018-06-21 14:51:23

标签: javascript css laravel intl-tel-input

我有一个Laravel 5.6网页,我想添加IntlTelInput (https://github.com/jackocnr/intl-tel-input),但我尝试从源代码,composer和npm(当前使用composer并混合复制生成文件夹)安装它,而其中3个似乎未初始化插件。 Example

  • 这不是我的浏览器(已经尝试了很多,并且可以在Codepen中使用)
  • 我已安装了jQuery
  • 我包括了样式表
  • 我在CSS中覆盖了flags.png的路径(在HTML和app.scss中[并运行npm run watch])
  • 我在输入之后添加了intlTelInput.js脚本
  • 我用<script>$("#phone").intlTelInput();</script>初始化了输入(在DOM中输入之后)

这是我从检查结果中得到的:

    <div class="intl-tel-input allow-dropdown">
       <div class="flag-container">
          <div tabindex="0" class="selected-flag">
             <div class="iti-flag"></div>
             <div class="iti-arrow"></div>
          </div>
          <ul class="country-list hide">
             <li data-dial-code="1" data-country-code="us" class="country preferred">
                <div class="flag-box">
                   <div class="iti-flag us"></div>
                </div>
                <span class="country-name">United States</span><span class="dial-code">+1</span>
             </li>
             <li data-dial-code="44" data-country-code="gb" class="country preferred">
                <div class="flag-box">
                   <div class="iti-flag gb"></div>
                </div>
                <span class="country-name">United Kingdom</span><span class="dial-code">+44</span>
             </li>
             <li class="divider"></li>
             <li data-dial-code="93" data-country-code="af" class="country ">
                <div class="flag-box">
                   <div class="iti-flag af"></div>
                </div>
                <span class="country-name">Afghanistan (‫افغانستان‬‎)</span><span class="dial-code">+93</span>
             </li>
             <li data-dial-code="355" data-country-code="al" class="country ">
                <div class="flag-box">
                   <div class="iti-flag al"></div>
                </div>
                <span class="country-name">Albania (Shqipëri)</span><span class="dial-code">+355</span>
             </li>
             <li data-dial-code="213" data-country-code="dz" class="country ">
                <div class="flag-box">
                   <div class="iti-flag dz"></div>
                </div>
                <span class="country-name">Algeria (‫الجزائر‬‎)</span><span class="dial-code">+213</span>
             </li>

....

                <span class="country-name">Western Sahara (‫الصحراء الغربية‬‎)</span><span class="dial-code">+212</span>
             </li>
             <li data-dial-code="967" data-country-code="ye" class="country ">
                <div class="flag-box">
                   <div class="iti-flag ye"></div>
                </div>
                <span class="country-name">Yemen (‫اليمن‬‎)</span><span class="dial-code">+967</span>
             </li>
             <li data-dial-code="260" data-country-code="zm" class="country ">
                <div class="flag-box">
                   <div class="iti-flag zm"></div>
                </div>
                <span class="country-name">Zambia</span><span class="dial-code">+260</span>
             </li>
             <li data-dial-code="263" data-country-code="zw" class="country ">
                <div class="flag-box">
                   <div class="iti-flag zw"></div>
                </div>
                <span class="country-name">Zimbabwe</span><span class="dial-code">+263</span>
             </li>
             <li data-dial-code="358" data-country-code="ax" class="country ">
                <div class="flag-box">
                   <div class="iti-flag ax"></div>
                </div>
                <span class="country-name">Åland Islands</span><span class="dial-code">+358</span>
             </li>
          </ul>
       </div>
       <input name="phone" id="phone" placeholder="Phone" value="9992715671" required="required" autocomplete="off" class="form-control" type="text">
    </div>

当我从ul class =“ country-list hide”手动删除hide类时,它会出现,但我无法与其交互,看来我的js无法正常工作。

如果我删除“隐藏”怎么办?

No Hide Class

更新:

进行更多测试,我意识到,如果我注释掉“ app.js ”脚本,则intl-tel-input可以正常工作,所以我的问题是,现在我需要在App.js中进行哪些更改使这项工作? (我没有在app.js中添加任何内容,这与每个laravel安装随附的相同)

2 个答案:

答案 0 :(得分:0)

通过纱线安装后,我遇到了类似的问题。 我的解决方案是添加以下内容:

require('intl-tel-input/build/js/utils'); require("intl-tel-input/build/js/intlTelInput");

./resources/assets/js/bootstrap.js之后到require('bootstrap');

然后添加:

jQuery( '#phone' ).intlTelInput({ dropdownContainer: 'body', autoPlaceholder: 'polite', initialCountry: 'auto', geoIpLookup: function(callback) { jQuery.get("https://ipinfo.io", function() {}, "jsonp").always(function(resp) { var countryCode = (resp && resp.country) ? resp.country : ""; callback(countryCode); }); }, });

到文件./resources/assets/js/app.js

的末尾

注意,我向我添加了一些论据,因为在进行自动整理之前,我在自动国家/地区检测方面也遇到了问题。

答案 1 :(得分:0)

我尝试删除defer属性,但出现此错误:

[Vue warn]: Cannot find element: #app

尝试将您的app.js导入项移到body标签之后。这样可以解决问题。

<body>...</body>

<!-- Scripts -->
<script src="{{ asset('js/app.js') }}"></script>