Home > Article > Web Front-end > How to use ajax to dynamically splice html code
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 += '<span class="unchecked" name="category" checked="false" onclick="change(this); " value='+category.id+' >'+category.name+'</span>'+ '<input type="hidden" id="category_id" name="category_id">'; } //将html动态拼接到对应的p上面 $('.category-list').html(str); } , error:function (XMLHttpRequest, textStatus, errorThrown) { alert("请求失败!"+XMLHttpRequest+"==="+textStatus+"==="+errorThrown); } }); }
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!