layui动态添加,删除下拉框组件,并且渲染数据
时间: 2024-05-03 16:22:24 浏览: 155
Layui带搜索的下拉框的使用以及动态数据绑定方法
在layui中实现动态添加、删除下拉框组件,并且渲染数据的步骤如下:
1. 引入layui组件库:
```html
<link rel="stylesheet" href="layui/css/layui.css">
<script src="layui/layui.js"></script>
```
2. 定义下拉框组件的HTML结构:
```html
<div class="layui-form-item">
<div class="layui-input-block">
<select name="select" lay-verify="required">
<option value="">请选择</option>
</select>
</div>
<div class="layui-input-block">
<button class="layui-btn layui-btn-sm layui-btn-danger" onclick="deleteSelect(this)">删除</button>
</div>
</div>
```
其中,`deleteSelect(this)`是动态删除下拉框的JavaScript函数。
3. 使用layui的form组件对下拉框进行渲染:
```javascript
layui.use(['form'], function(){
var form = layui.form;
form.render('select');
});
```
4. 使用jQuery动态添加下拉框组件:
```javascript
function addSelect() {
var html = '<div class="layui-form-item">' +
'<div class="layui-input-block">' +
'<select name="select" lay-verify="required">' +
'<option value="">请选择</option>' +
'</select>' +
'</div>' +
'<div class="layui-input-block">' +
'<button class="layui-btn layui-btn-sm layui-btn-danger" onclick="deleteSelect(this)">删除</button>' +
'</div>' +
'</div>';
$('#select-container').append(html);
layui.use(['form'], function(){
var form = layui.form;
form.render('select');
});
}
```
其中,`#select-container`是包含下拉框组件的容器元素。
5. 使用jQuery动态删除下拉框组件:
```javascript
function deleteSelect(button) {
$(button).parents('.layui-form-item').remove();
}
```
完整代码如下:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>动态添加、删除下拉框组件</title>
<link rel="stylesheet" href="layui/css/layui.css">
</head>
<body>
<div class="layui-container">
<div class="layui-row">
<div class="layui-col-md12">
<fieldset class="layui-elem-field">
<legend>动态添加、删除下拉框组件</legend>
<div class="layui-field-box">
<div class="layui-form" lay-filter="form">
<div id="select-container">
<div class="layui-form-item">
<div class="layui-input-block">
<select name="select" lay-verify="required">
<option value="">请选择</option>
<option value="1">选项1</option>
<option value="2">选项2</option>
<option value="3">选项3</option>
<option value="4">选项4</option>
<option value="5">选项5</option>
</select>
</div>
<div class="layui-input-block">
<button class="layui-btn layui-btn-sm layui-btn-danger" onclick="deleteSelect(this)">删除</button>
</div>
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" onclick="addSelect()">添加下拉框</button>
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn layui-btn-normal" lay-submit lay-filter="submit">提交</button>
</div>
</div>
</div>
</div>
</fieldset>
</div>
</div>
</div>
<script src="layui/layui.js"></script>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
layui.use(['form'], function(){
var form = layui.form;
form.render('select');
form.on('submit(submit)', function(data){
console.log(data.field);
return false;
});
});
function addSelect() {
var html = '<div class="layui-form-item">' +
'<div class="layui-input-block">' +
'<select name="select" lay-verify="required">' +
'<option value="">请选择</option>' +
'<option value="1">选项1</option>' +
'<option value="2">选项2</option>' +
'<option value="3">选项3</option>' +
'<option value="4">选项4</option>' +
'<option value="5">选项5</option>' +
'</select>' +
'</div>' +
'<div class="layui-input-block">' +
'<button class="layui-btn layui-btn-sm layui-btn-danger" onclick="deleteSelect(this)">删除</button>' +
'</div>' +
'</div>';
$('#select-container').append(html);
layui.use(['form'], function(){
var form = layui.form;
form.render('select');
});
}
function deleteSelect(button) {
$(button).parents('.layui-form-item').remove();
}
</script>
</body>
</html>
```
阅读全文