简体   繁体   English

使用 Alpine.js 将输入文本更改为大写

[英]Change input text to upper case using Alpine.js

I would like every character entered into my HTML input box to get uppercased.我希望输入到我的 HTML input框中的每个字符都大写。 Here's the approach I am trying using the latest Alpine.js available via CDN.这是我尝试使用通过 CDN 提供的最新 Alpine.js 的方法。

<input 
   x-data="{myText: '' }" 
   x-text="myText" type="text" 
   @keydown="myText.toUpperCase();" 
   name="myText" 
   placeholder="Some Text"/>

This seems to have zero effect.这似乎有零影响。 What's the right way of doing solving this problem?解决这个问题的正确方法是什么?

There are a couple of things going on.有几件事正在发生。 First of all, you're binding to keydown but not assigning your uppercased value to any reactive property.首先,您绑定到 keydown 但未将大写值分配给任何反应性属性。 Second is that you probably want to use x-bind:value .其次是您可能想要使用x-bind:value

<input 
   x-data="{myText: '' }" 
   type="text" 
   @keyup="myText = $event.target.value.toUpperCase()"
   :value="myText"
   name="myText" 
   placeholder="Some Text"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM