如何在React JS中显示H1中的输入值

发布于 2025-02-11 20:27:17 字数 298 浏览 2 评论 0原文

我有一个划分2个数字并给出值的输入,因此我想在H1 Tag

 <input className='input' type="number" id="" placeholder='$' onChange={(evt) => {console.log(evt.target.value / coinId.price)}}/>

coinid中显示该值。预点是todays btc Price与API的价格,但在这一点上,这不是重要的

代码,这使我获得了控制台日志的结果。有什么建议吗?谢谢。

I have one input which divides 2 number and gives value, so I want to display that value in h1 tag

 <input className='input' type="number" id="" placeholder='

coinId.price is todays BTC price from API but it is not important at this point

here is code which gave me result in console log so any suggestions? Thanks.

onChange={(evt) => {console.log(evt.target.value / coinId.price)}}/>

coinId.price is todays BTC price from API but it is not important at this point

here is code which gave me result in console log so any suggestions? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌︼了一个春 2025-02-18 20:27:17

我认为您忘记了value属性,如果您使用React Hooks,也许您可​​以做这样的事情:

import { useState } from "react";

const [inputValue, setInputValue] = useState("");

...


<input className="input" type="number" placeholder="$" onChange={(evt) => setInputValue(evt.target.value / coinId.price)} value={inputValue} />```

You forgot the value attribute I think, and if you use React Hooks maybe you can do something like this :

import { useState } from "react";

const [inputValue, setInputValue] = useState("");

...


<input className="input" type="number" placeholder="
quot; onChange={(evt) => setInputValue(evt.target.value / coinId.price)} value={inputValue} />```
魔法少女 2025-02-18 20:27:17

我能想到的一种方法之一是,通过使用Usestate,设置一个状态

const(myans,setMyans)= usestate(0)

&lt; input className ='input'type =“ number” id =“”占位符='$'onChange = {(evt)=&gt; {setMyans(evt.target.value/coinid.price)}}}/&gt;

&lt; h1&gt; {myans}&lt;/h1&gt;

You can also use useEffect, so that whenever price of bitcoin is changed, you may get updated value.

或者

&lt; h1&gt;&lt; input className ='input'type =“ number” id =“”占位符='$'onChange = {(evt)=&gt; {setMyans(evt.target.value/coinid.price)}}} value = {inputValue}/&gt;&lt;/h1&gt;

For furthur reference if case,

One of the methods that I can think of is that, by using useState, set a state like

const (myAns,setMyAns) = useState(0)

<input className='input' type="number" id="" placeholder='$' onChange={(evt) => {setMyAns(evt.target.value / coinId.price)}}/>

<h1>{myAns}</h1>

You can also use useEffect, so that whenever price of bitcoin is changed, you may get updated value.

Or

<h1><input className='input' type="number" id="" placeholder='$' onChange={(evt) => {setMyAns(evt.target.value / coinId.price)}} value={inputValue} /></h1>

For furthur reference if case, how to make a input field value in h1?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文