简体   繁体   English

AJAX查询后的格式字段

[英]Format field after AJAX query

I am trying to get my field to format after data is placed in it from an AJAx query. 我试图通过AJAx查询将数据放入字段后进行格式化。 I just figured out how to use Ajax about 20 minutes ago so I'm fairly noobish still towards it. 大约20分钟前,我刚刚想出了如何使用Ajax,所以我仍然对它还不满意。

 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. 它基于单独字段中提供的SN返回MAC地址。 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. 该调用正常运行,并且我拥有用于​​格式化字段的代码,但它基于Key Up和Change事件。

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. 我不确定从ajax调用将数据放入其中时如何自动运行该函数。

Any and all help is much appreciated. 任何帮助都将不胜感激。 Thanks! 谢谢!

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. 如果要保留onkeyupchange处理程序的当前设置,可以通过“伪造” keyup事件将生成的格式来调用该函数。 I would suggest to refactor the event handler though, so that the formatMAC function is more general. 我建议尽管重构事件处理程序,以使formatMAC函数更通用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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