weixin_33728708 2015-08-25 09:34 采纳率: 0%
浏览 20

AJAX查询后的格式字段

I am trying to get my field to format after data is placed in it from an AJAx query. I just figured out how to use Ajax about 20 minutes ago so I'm fairly noobish still towards it.

 document.getElementById("mpMAC").value = xmlhttp.responseText;

Above is the field that needs to be formatted. It returns a MAC address based on a SN provided in a separate field. The call is functioning properly and I have my code that I've been using to format the field but it is based on Key Up and Change events.

Here is my code for the formatting part that I've been using for a few weeks now

function formatMAC(e) {
var r = /([a-f0-9]{2})([a-f0-9]{2})/i,
    str = e.target.value.replace(/[^a-f0-9]/ig, "");
while (r.test(str)) {
    str = str.replace(r, '$1' + ':' + '$2');
}
e.target.value = str.slice(0, 17);
};
$("#mpMAC").on("keyup change", formatMAC); 

I'm not sure how to run the function automatically when the data is placed in there from the ajax call.

Any and all help is much appreciated. Thanks!

  • 写回答

1条回答 默认 最新

  • weixin_33701251 2015-08-25 09:40
    关注

    If you want to keep your current setup for the onkeyupchange handler, you can call the function by 'faking' the format a keyup event will generate. I would suggest to refactor the event handler though, so that the formatMAC function is more general.

    var node = document.getElementById("mpMAC"),
        pack = {
            'target' : {
                'value' : xmlhttp.responseText
            }
        },
        text = formatMAC(pack);
    node.value = pack.target.value;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料