不要遍历dom

时间:2021-03-18 06:13:53

function selectProvince() {
$.ajax(
{
type: "post",
url: "/province/getStrType",
data: { "type": "province" },
success: function (msg) {
for (var i = 0; i < msg.length; i++) {
$("#province").append("<option value=" + msg[i].CODE + ">" + msg[i].PROVINCE + "</option>");
}
selectCity();
}
})
};

上面这种写法遍历dom,不好,具体哪不好,以后研究,应该下面的写法

function selectProvince() {
$.ajax(
{
type: "post",
url: "/province/getStrType",
data: { "type": "province" },
success: function (msg) {
alert(msg);

var str;
for (var i = 0; i < msg.length; i++) {
str+="<option value=" + msg[i].CODE + ">" + msg[i].PROVINCE + "</option>";
}
$("#province").append(str);
selectCity();
}
})
};