1 var tel;
2 var ajax=function(){
3 $.ajax({
4 type: "get",
5 url: 'http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel='+tel,
6 dataType: "jsonp", //jsonp跨域
7 jsonp: "callback",
8 success: function(data){
9 console.log(data);
10 $('.error').css('display','none');
11 var province = data.province,
12 operators = data.catName,
13 num = data.telString;
14
15 $('.num span').html(num);
16 $('.province span').html(province);
17 $('.operators span').html(operators);
18 },
19 error:function (){
20 $('li span').html('');
21 $('.error').css('display','block');
22 }
23 });
24 }
25 var reg = /^(13|15|18)[0-9]{9}$/;//点击查询
26 $('.button').click(function(){
27 tel=$('input[type=text]').val();
28 if(tel){
29 if(reg.test(tel)){
30 ajax();
31 }else{
32 $('li span').html('');
33 $('.error').css('display','block');
34 }
35 }
36 });
37 //键盘事件
38 $(window).keydown(function(event){
39 tel=$('input[type=text]').val();
40 if(event.keyCode==13) {
41 if(tel){
42 if(reg.test(tel)){
43 ajax();
44 }else{
45 $('li span').html('');
46 $('.error').css('display','block');
47 }
48 }
49 }
50 });