ksc010
V2EX  ›  HTML

[前端] 若一个 input 被双向数据绑定了,如何通过外部 js 设置该 input 的 value

  •  
  •   ksc010 · Nov 4, 2023 · 2895 views
    This topic created in 928 days ago, the information mentioned may be changed or developed.
    <input type="text" id="uname"  v-model="uname" />
    

    我需要编写一个插件自动,设置 uname 的值; 通过 ele.value='abc',可以设置上 但是,鼠标点击 input 输入框后,就被还原了,应该和 类似 Vue 这种框架的双向绑定有关系;

    然后搜到了如下的方法,但是没有生效

    $input = document.querySelector('#uname)
    $input.value='abc'
    $input.dispatchEvent(new Event('input', {bubbles:true}))
    
    2 replies    2023-11-04 15:18:19 +08:00
    Puteulanus
        1
    Puteulanus  
       Nov 4, 2023
    ksc010
        2
    ksc010  
    OP
       Nov 4, 2023
    @Puteulanus 感谢,刚发现 上面代码又可以了 可能遗漏了什么一开始
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1474 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 58ms · UTC 17:01 · PVG 01:01 · LAX 10:01 · JFK 13:01
    ♥ Do have faith in what you're doing.