Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当特别指定为type=”时,输入字段值的默认类型是什么;数字;?_Javascript_Html - Fatal编程技术网

Javascript 当特别指定为type=”时,输入字段值的默认类型是什么;数字;?

Javascript 当特别指定为type=”时,输入字段值的默认类型是什么;数字;?,javascript,html,Javascript,Html,我使用带有React钩子和上下文Api的reactJs,在正常的html表单中,我必须输入类型为text的标记,当我试图知道javascript中的“typeOf”函数使用的是什么类型的值时。当我输入字符串值时,我得到了字符串类型,但当我输入一个数字时,它显示为未定义 const [name, setName] = useState(''); const [price, setPrice] = useState(''); const [movies , setMovie] = useContex

我使用带有React钩子和上下文Api的reactJs,在正常的html表单中,我必须输入类型为text的标记,当我试图知道javascript中的“typeOf”函数使用的是什么类型的值时。当我输入字符串值时,我得到了字符串类型,但当我输入一个数字时,它显示为未定义

const [name, setName] = useState('');
const [price, setPrice] = useState('');
const [movies , setMovie] = useContext(MovieContext) 

const updateName = (e) => {
    setName(e.target.value)
}

const updatePrice = (e) => {
    setPrice(e.target.price);
}

const addMovie = (e) => {
    e.preventDefault();
    setMovie(prevMovies => {
       return [...prevMovies, { name: name, price: price }]
    })
    console.log(typeof(price)) //getting undefined
    console.log(typeof(name)) //getting string 
}

return ( 
    <div>
       <form onSubmit={addMovie}>
           <input type="text"  name="name" value={name} onChange={ updateName }/>
           <input type="text"  name="price" value={price} onChange={ updatePrice } />
           <button>Submit</button>
       </form>
    </div>
 );
const[name,setName]=useState(“”);
const[price,setPrice]=使用状态(“”);
const[movies,setMovie]=useContext(MovieContext)
常量updateName=(e)=>{
setName(例如target.value)
}
const updatePrice=(e)=>{
设定价格(即目标价格);
}
const addMovie=(e)=>{
e、 预防默认值();
setMovie(prevMovies=>{
return[…prevMovies,{name:name,price:price}]
})
console.log(typeof(price))//未定义
console.log(typeof(name))//获取字符串
}
报税表(
提交
);

由于没有存储值的
price
属性,因此应将值解析为数字,以便将值作为数字获取

setPrice(Number(e.target.value));

此外,您还可以将输入类型更改为数字。

HTML只有一种数据类型:文本(字符串)。

从HTML元素提取的所有值都是字符串。如果您需要以不同的方式对待某个值,那么转换类型将取决于您的JavaScript。对于数值数据,有几种方法,但可能最简单的方法是在值前面加一个算术运算符

setPrice(Number(e.target.value));
let input=document.querySelector(“输入”);
addEventListener(“blur”,function()){
日志(“数据为:”+typeof input.value);
console.log(“转换的类型为:”+typeof+input.value);
});

输入一个数字,然后按TAB:
你的意思是说
输入值是strings吗?值得补充的是,这里也有属性,尽管这里已经说明了这一点。@DavidThomas还有
number()
,我觉得
parseInt()
parseFloat()
,我没有涵盖它,一元
+
就可以了。我希望价格值是字符串,因为在useState(“”)中,我将其定义为空字符串,将任何更新的值都视为字符串。但当我在JavaScript中使用typeof(price)函数登录时…它显示为未定义。我希望该price值为string,因为在useState(“”)中,我将其定义为空字符串,将任何更新的值仅视为string。但是当我用JavaScript中的typeof(price)函数登录时…它显示为未定义。而不是
e.target.price
使用
e.target.value
哦,不,这是我的错误。谢谢你的帮助。