Home >Web Front-end >JS Tutorial >Dynamically add option to select in layui
This article mainly shares an example of dynamically adding options to layui select. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor to take a look, I hope it can help everyone.
html
<form class="layui-form" action=""> <p class="layui-form-item proSelect"> <label class="layui-form-label">产品类别</label> <p class="layui-input-block editWidth"> <select name="productList" lay-verify="required" id="zcySelect"> <option value=""></option> <option value="0">轻松融</option> <option value="1">容易融</option> <option value="2">快乐融</option> </select> </p> </p> <a class="layui-btn layui-btn-small" id="" onclick="addProductClassify()">增加产品类别</a> </form> <!--弹窗内容--> <p id="select_prod" class="layui-form" hidden="hidden"> <p class="layui-input-inline"> <input type="text" name="text" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input"> </p> </p>
js
//重新渲染表单 function renderForm(){ layui.use('form', function(){ var form = layui.form();//高版本建议把括号去掉,有的低版本,需要加() form.render(); }); } //增加产品类别按钮点击事件 function addProductClassify(){ layer.open({ type:1, btn:['确定','取消'], content:$("#select_prod"), area:['270px','160px'], //当前层索引参数(index)、当前层的DOM对象(layero) yes:function(index,layero){ //获取input输入的值 var ivalue=$(layero).find("input").val(); //获取要添加的option的索引 var sIndex=$("#zcySelect")[0].options.length-1; if(ivalue==null||ivalue==''){ layer.msg("请输入产品类别") } else{ layer.msg("输入的产品类别是:"+ivalue); //为select添加option $("#zcySelect").append("<option value="+sIndex+">"+ivalue+"</option>"); renderForm();//表单重新渲染,要不然添加完显示不出来新的option layer.close(index); } $(layero).find("input").val(''); } }) }
Related recommendations:
detailed explanation of dynamically adding options to datalist or select using js
JS dynamically add option and delete option (with example code)_javascript skills
jquery dynamically add option example_jquery
The above is the detailed content of Dynamically add option to select in layui. For more information, please follow other related articles on the PHP Chinese website!