简体   繁体   中英

Selecting Telephone Number JQuery does not select the first '0'

I have a simple jQuery selector which is supposed to select a telephone number from a data attribute when a link is clicked.

<a class="mobile" href="javascript:void(0);" data-mobile_p="064366554565">Send SMS</a>

When selected with the following,

$('.mobile').data('mobile_p');
alert(mobile);

The returned number is 64366554565 - without the beginning 0. Albeit I can then easily add another '0' to it but this does not seem good practice.

For me it works fine. jsfiddle

$('.mobile').click(function () {
   var mobile = $(this).data('mobile_p');
   alert(mobile);
});

Well, according to your comment, you are probably assigning variable mobile somewhere out of function scope. Assign local variable inside.

$(document).on(
    'click', 
    '.mobile',
    function() {
        var mobile = $('.mobile').data('mobile_p'); 
        alert(mobile);
    }​);​

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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