<script>
layui.use(['form', 'layedit', 'laydate'], function () {
var $ = layui.jquery;
$.ajax({
url: '../api/SysType/GetArticleType?parentId=1',//json文件位置
type: 'get', //请求方式为get
dataType: 'json', //返回数据格式为json
success: function (result) { //请求成功完成后要执行的方法
var select = $("#TypeID");//TypeID为HTML标签ID
$.each(result, function (index, item) {
select.append(new Option( item.TypeName, item.TypeID));// 下拉菜单里添加元素
});
layui.form.render("select");
}
});
});</script>
这段代码展示了如何利用layui库通过Ajax从后台获取文章类型数据,并填充到HTML的下拉菜单中。首先引入layui的form、layedit和laydate模块,然后使用jQuery发起GET请求到指定URL,接收到JSON数据后遍历并构造新的选项,最后使用layui的form渲染函数更新select元素。

5855

被折叠的 条评论
为什么被折叠?



