Home > Web Front-end > JS Tutorial > How to use ajax to dynamically splice html code

How to use ajax to dynamically splice html code

一个新手
Release: 2017-10-01 07:44:16
Original
2928 people have browsed it

function get_all_category_with_id() {
    $.ajax( {
    type: "get", url: "../../../appUpload/getAllCategoryByid", async : false, dataType: "text", success: function (data) {
    var obj = JSON.parse(data);
    var str ="";
    //先将元素对应清空 $('.category-list').empty();
    for(var i = 0;
    i<obj.length;
    i++) {
    var category = obj[i];
    str += &#39;<span class="unchecked" name="category" checked="false" onclick="change(this);
    " value=&#39;+category.id+&#39; >&#39;+category.name+&#39;</span>&#39;+ &#39;<input type="hidden" id="category_id" name="category_id">&#39;;
}
//将html动态拼接到对应的p上面 $(&#39;.category-list&#39;).html(str);
}
, error:function (XMLHttpRequest, textStatus, errorThrown) {
    alert("请求失败!"+XMLHttpRequest+"==="+textStatus+"==="+errorThrown);
}
});
}
Copy after login

The above is the detailed content of How to use ajax to dynamically splice html code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template