美文网首页
解析字符中的变量动态填充input输入框

解析字符中的变量动态填充input输入框

作者: Amanda妍 | 来源:发表于2023-08-16 14:51 被阅读0次

"您的验证码为{code},有效期3分钟,若非本人操作,请勿泄露。有问题联系我们{tel}"

//解析的关键方法
 smsTemplateData() {
      let str = this.templateStr
      let reg = /\$\{(.+?)\}/
      let reg_g = /\$\{(.+?)\}/g
      let result = str.match(reg_g)
      let list = []
    //解析${}中的变量组成数组(我们项目中${name}不用叫填写所以加了判断)
      for (let i = 0; i < result.length; i++) {
        let item = result[i]
        if (item === '${name}') str = str.replace('${name}', '***')
        else list.push(item.match(reg)[1])
      }
      this.templateVariableList = list
      if (list.length > 0) {
        list.forEach(item => {
          str = str.replace('${' + item + '}', `<input class="self-input" id="${item}" />`)
        })
      }
      this.templateStr = str
    },
//提交表单的时候 获取·动态生成的Input的值

 let obj = {}
          let isFull = false
          // 短信模版变量封装 js生成的输入框不能通过v-model绑定值,我是通过原生办法获取值的
          if (this.templateVariableList.length > 0) {
            this.templateVariableList.forEach(item => {
              let realValue = document.getElementById(item).value
              if (!realValue) isFull = true
            })
            if (isFull) return Toast('请完善通知内容!')
            this.templateVariableList.forEach(item => {
              let realValue = document.getElementById(item).value
              obj[item] = realValue
            })
          }
          this.model.templateData = obj
image.png

相关文章

网友评论

      本文标题:解析字符中的变量动态填充input输入框

      本文链接:https://www.haomeiwen.com/subject/gzgkmdtx.html