Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery单击或touchstart事件在移动设备上不起作用_Javascript_Jquery_Mobile_Onclick_Jquery Events - Fatal编程技术网

Javascript jQuery单击或touchstart事件在移动设备上不起作用

Javascript jQuery单击或touchstart事件在移动设备上不起作用,javascript,jquery,mobile,onclick,jquery-events,Javascript,Jquery,Mobile,Onclick,Jquery Events,我用它来获取人们的电话号码和国家。当他们选择国旗和代码时,我用jQuery分别获取国家和代码,并填充隐藏字段 以下是处理这些操作的整个对象: var telInput = { init: function(){ this.setTelInput(); this.cacheDOM(); this.bindEvents(); }, cacheDOM: function(){ this.$countryCode =

我用它来获取人们的电话号码和国家。当他们选择国旗和代码时,我用jQuery分别获取国家和代码,并填充隐藏字段

以下是处理这些操作的整个对象:

var telInput = {
    init: function(){
        this.setTelInput();
        this.cacheDOM();
        this.bindEvents();
    },
    cacheDOM: function(){
        this.$countryCode = $('input[name=country_code]');
        this.$countryName = $('input[name=country_name]');
        this.$country     = $('.country');
    },
    bindEvents: function(){
        this.$country.on('click', this.getTelInput.bind(this));
    },
    setTelInput: function(){
        $('input[name=phone]').intlTelInput({
            preferredCountries: [
                'gb'
            ],
        });
    },
    getTelInput: function(e){
        var el          = $(e.target).closest('.country');
        var countryCode = el.attr('data-dial-code');
        var countryName = el.find('.country-name').text();
        this.$countryCode.val(countryCode);
        this.$countryName.val(countryName);
    },
}
问题


问题是bindEvents方法中的click事件。。。电话选择器可以工作,但不会触发onclick事件来填充我的隐藏字段。

您需要确保在呈现DOM后缓存它。这里有一个简化的例子

此外,插件的事件在您的情况下可能很有用

$("#country-select-element").on("countrychange", function(e, countryData) {
    // do something with countryData
});
我还建议你看一看。它有一些有用的方法,您可以使用它们来实现相同的结果。

在呈现DOM后是否调用cacheDOM方法?
$("#country-select-element").on("countrychange", function(e, countryData) {
    // do something with countryData
});