解析字符中的變量動(dòng)態(tài)填充input輸入框

"您的驗(yàn)證碼為{code},有效期3分鐘,若非本人操作,請(qǐng)勿泄露。有問題聯(lián)系我們{tel}"

//解析的關(guān)鍵方法
 smsTemplateData() {
      let str = this.templateStr
      let reg = /\$\{(.+?)\}/
      let reg_g = /\$\{(.+?)\}/g
      let result = str.match(reg_g)
      let list = []
    //解析${}中的變量組成數(shù)組(我們項(xiàng)目中${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
    },
//提交表單的時(shí)候 獲取·動(dòng)態(tài)生成的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('請(qǐng)完善通知內(nèi)容!')
            this.templateVariableList.forEach(item => {
              let realValue = document.getElementById(item).value
              obj[item] = realValue
            })
          }
          this.model.templateData = obj
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容