当前位置:   article > 正文

elementui el-input,v-for循环、校验、消除校验_el-input v-for

el-input v-for

第一步;页面 el-input循环

  1. <template>
  2. <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
  3. <div v-for="(item,index) in ruleForm.mailSet" :key="index">
  4. <el-form-item >
  5. <el-input v-model.trim="ruleForm.mailSet[index]" ></el-input>
  6. <img src="add" @click="addItem">
  7. <img src="del" @click="deleteItem">
  8. </el-form-item>
  9. </div>
  10. </el-form>
  11. <template>
  12. <script>
  13. export default{
  14. data(){
  15. return {
  16. ruleForm:{
  17. mailSet:[],
  18. }
  19. }
  20. },
  21. }
  22. </script>

第二步:添加按钮(增加input框 or 减少input框)

  1. <template>
  2. <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
  3. <div v-for="(item,index) in ruleForm.mailSet" :key="index">
  4. <el-form-item >
  5. <el-input v-model.trim="ruleForm.mailSet[index]" ></el-input>
  6. <img src="add" @click="addItem">
  7. <img src="del" @click="deleteItem">
  8. </el-form-item>
  9. </div>
  10. </el-form>
  11. <template>
  12. <script>
  13. export default{
  14. data(){
  15. return {
  16. ruleForm:{
  17. mailSet:[],
  18. }
  19. }
  20. },
  21. methods:{
  22. //添加一个input
  23. addItem(){
  24. this.ruleForm.mailSet.splice(index+1,0,"")
  25. },
  26. //删除一个input
  27. deleteItem(){
  28. this.ruleForm.mailSet.splice(index,1)
  29. },
  30. },
  31. }
  32. </script>

第三步:对input 框进行校验(注意:自定义校验 callback 必须被调用。)

  1. <template>
  2. <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
  3. <div v-for="(item,index) in ruleForm.mailSet" :key="index">
  4. <el-form-item :prop="'mailSet.'+index" :rules="rules.mailSetRules">
  5. <el-input v-model.trim="ruleForm.mailSet[index]" ></el-input>
  6. <img src="add" @click="addItem">
  7. <img src="del" @click="deleteItem">
  8. </el-form-item>
  9. </div>
  10. </el-form>
  11. <template>
  12. <script>
  13. export default{
  14. data(){
  15. return {
  16. ruleForm:{
  17. mailSet:[],
  18. },
  19. rules:{
  20. mailSetRules:[{ message: '请填写电子邮箱', trigger: 'blur'},
  21. {  trigger: ["blur", "change"],
  22. validator:(rule, value, callback) => {
  23. let set1=new Set(this.ruleForm.mailSet);
  24. var reg=/^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
  25. if(!reg.test(value) && this.ruleForm.isMail==2){
  26. this.mailSetRule=false;
  27. return callback("邮箱格式不正确");
  28. }
  29. if(this.ruleForm.mailSet.length!=set1.size){
  30. this.mailSetRule=false;
  31. return callback("邮箱不可重复");
  32. }
  33. callback();
  34. }
  35. }
  36. ],
  37. },
  38. }
  39. },
  40. }
  41. </script>

第四步:如果多个页面共用一张表的话,消除校验回显(添加一个v-if判断条件,必须加上key值)

  1. <template>
  2. <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
  3. <div v-for="(item,index) in ruleForm.mailSet" :key="index">
  4. <el-form-item :prop="'mailSet.'+index" :rules="rules.mailSetRules" v-if="" >
  5. <el-input v-model.trim="ruleForm.mailSet[index]" ></el-input>
  6. <img src="add" @click="addItem">
  7. <img src="del" @click="deleteItem">
  8. </el-form-item>
  9. <el-form-item :prop="'mailSet.'+index" :rules="rules.mailSetRules" v-else=""
  10. key="1">
  11. <el-input v-model.trim="ruleForm.mailSet[index]" ></el-input>
  12. <img src="add" @click="addItem">
  13. <img src="del" @click="deleteItem">
  14. </el-form-item>
  15. </div>
  16. </el-form>
  17. <template>

效果图

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/115769
推荐阅读
相关标签