0

我有来自 db 的 asp:Label phone 字段绑定。我需要添加破折号来制作像 999-999-9999 而不是 99999999999 这样的电话格式。我尝试了类似的方法,但它不起作用:

$(document).ready(function ()  
    num = $('#<%=HomePhone.ClientID %>').innerText;
    parts = [num.slice(0,3),num.slice(3,6),num.slice(6,10)];
    fNum = parts[0]+"-"+parts[1]+"-"+parts[2];
    num = fNum;
});
4

2 回答 2

0

将您的代码更改为:

$(document).ready(function () {
    num = $('#<%=HomePhone.ClientID %>').text(); // jQuery does not know innerText
    parts = [num.slice(0,3),num.slice(3,6),num.slice(6,10)];
    fNum = parts[0]+"-"+parts[1]+"-"+parts[2];
    //num = fNum; // setting a variable's value does not change value of your text box
    $('#<%=HomePhone.ClientID %>').text(fNum); // set the value using jQuery
});
于 2012-08-14T14:24:55.717 回答
0

HTML:

<p id="textbox">99999999999</p>

Javascript:

$(function () {
    var num = $('#textbox').html();
    var parts = [num.slice(0, 3), num.slice(3, 6), num.slice(6)];
    var fNum = parts[0] + "-" +parts[1] + "-" + parts[2];
    $('#textbox').html(fNum);
});​

小提琴:

http://jsfiddle.net/timothyclifford/h9ZCR/

于 2012-08-14T14:17:30.733 回答