vue2+bootstrap弹窗可编辑表单
时间: 2023-07-29 19:09:44 浏览: 54
同样地,我们可以在Vue2项目中使用Bootstrap弹窗来实现可编辑表单的功能。以下是一个简单的示例:
```
<template>
<div>
<!-- 弹窗按钮 -->
<button type="button" class="btn btn-primary" @click="openModal">打开弹窗</button>
<!-- 弹窗 -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel">{{ title }}</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close" @click="closeModal"><span aria-hidden="true">×</span></button>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="inputName">姓名</label>
<input type="text" class="form-control" id="inputName" placeholder="请输入姓名" v-model="name">
</div>
<div class="form-group">
<label for="inputEmail">邮箱</label>
<input type="email" class="form-control" id="inputEmail" placeholder="请输入邮箱" v-model="email">
</div>
<div class="form-group">
<label for="inputPassword">密码</label>
<input type="password" class="form-control" id="inputPassword" placeholder="请输入密码" v-model="password">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" @click="closeModal">关闭</button>
<button type="button" class="btn btn-primary" @click="saveData">保存</button>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
title: '编辑表单',
name: '',
email: '',
password: ''
}
},
methods: {
openModal() {
$('#myModal').modal('show');
},
closeModal() {
$('#myModal').modal('hide');
},
saveData() {
// 在这里处理保存数据的逻辑
this.closeModal();
}
}
}
</script>
```
在上面的示例中,我们使用了Bootstrap的modal组件来实现弹窗的功能。在Vue组件中,我们可以通过点击按钮来打开弹窗,并在弹窗中嵌入一个表单。表单的各个输入框使用v-model指令来绑定数据,在保存数据时,我们可以在saveData方法中编写逻辑来处理数据的保存。
需要注意的是,为了在Vue中使用jQuery来控制Bootstrap组件,我们需要在项目中引入jQuery和Bootstrap的相关JS文件。可以在Vue项目的public/index.html文件中引入这些文件:
```
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
<div id="app"></div>
<!-- 引入jQuery和Bootstrap的JS文件 -->
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.3/js/bootstrap.min.js"></script>
<script src="/js/app.js"></script>
</body>
</html>
```
在上面的示例中,我们使用的是Bootstrap 4版本的JS文件,如果你使用的是其他版本的Bootstrap,请相应地修改引入的JS文件的路径。