我有asp:来自db的标签电话字段绑定。我需要添加破折号以使手机格式如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;
});
答案 0 :(得分: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);
});
<强>小提琴:强>
答案 1 :(得分: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
});