js 电话号码加空格 3-4-4 Vue

电话号码空格
监听input的keyup和keydown事件

mobileKeyUp (key) {
  if ((this.familyInfo.contact_no.length === 3 || this.familyInfo.contact_no.length === 8) && key.keyCode !== 8) {
    this.familyInfo.contact_no = this.familyInfo.contact_no + ' '
  } else if ((this.familyInfo.contact_no.length === 4 || this.familyInfo.contact_no.length === 9) && key.keyCode === 8) {
    this.familyInfo.contact_no = this.familyInfo.contact_no.substr(0, this.familyInfo.contact_no.length - 1)
  }
  if (this.shouldAddSpace) {
    let lastNewStr = this.familyInfo.contact_no.charAt(this.familyInfo.contact_no.length - 1)
    this.familyInfo.contact_no = this.familyInfo.contact_no.substr(0, this.familyInfo.contact_no.length - 1)
    this.familyInfo.contact_no = this.familyInfo.contact_no + ' ' + lastNewStr
    this.shouldAddSpace = false
  }
},
mobileKeyDown (key) {
  if ((this.familyInfo.contact_no.length === 3 || this.familyInfo.contact_no.length === 8) && key.keyCode !== 8) {
    this.shouldAddSpace = true
  }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  •   JavaScript 与 HTML 之间的交互是通过事件实现的。   事件,就是文档或浏览器窗口中发生的一些特...
    霜天晓阅读 3,556评论 1 11
  • 总结: 鼠标事件 1.click与dbclick事件$ele.click()$ele.click(handler(...
    阿r阿r阅读 1,646评论 2 10
  • 本篇博客源地址 总结: 鼠标事件 1.click与dbclick事件ele.click()ele.click(ha...
    ZombieBrandg阅读 703评论 0 1
  • (续jQuery基础(1)) 第5章 DOM节点的复制与替换 (1)DOM拷贝clone() 克隆节点是DOM的常...
    凛0_0阅读 1,386评论 0 8
  • 第1章 鼠标事件 1-1 jQuery鼠标事件之click与dbclick事件 用交互操作中,最简单直接的操作就是...
    mo默22阅读 1,310评论 0 6