admin管理员组文章数量:1293455
This code works perfectly in HTML
<input type="text" name="country" pattern="[A-z]{3}"
oninput="if (typeof this.reportValidity === 'function') {this.reportValidity();}"/>
How do I use it in Svelte. oninput is not allowed. I tried on:input, but I don't have access to this
This code works perfectly in HTML
<input type="text" name="country" pattern="[A-z]{3}"
oninput="if (typeof this.reportValidity === 'function') {this.reportValidity();}"/>
How do I use it in Svelte. oninput is not allowed. I tried on:input, but I don't have access to this
Share Improve this question asked Feb 12 at 17:08 ZAkyZAky 1,3099 silver badges25 bronze badges1 Answer
Reset to default 1You need to pass in a function via { ... }
, e.g.
<input type="text" name="country" pattern="[A-z]{3}"
oninput={function() { this.reportValidity(); }} />
this
is just the element, you can also get that from the event object, e.g.
<input type="text" name="country" pattern="[A-z]{3}"
oninput={e => e.target.reportValidity()} />
Using the event object is more common, it is also helpful for event delegation (the target
can be a descendant from which the event originated).
See:
target
currentTarget
(always the element the handler was set on)
For longer or reused functions, it is recommended to put them in the <script>
and reference them.
<script>
function validateCountry(e) {
e.target.reportValidity();
}
</script>
<input type="text" name="country-a" pattern="[A-z]{3}"
oninput={validateCountry} />
<input type="text" name="country-b" pattern="[A-z]{3}"
oninput={validateCountry} />
本文标签: How do I use oninput thisreportValidity in svelteStack Overflow
版权声明:本文标题:How do I use oninput this.reportValidity in svelte - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741580345a2386535.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论