简体   繁体   English

类型错误:无法读取 null reactJs 的属性“值”

[英]TypeError: Cannot read property 'value' of null reactJs

I am getting the following error TypeError: Cannot read property 'value' of null what do I need to do with value in this case please help me.我收到以下错误 TypeError: Cannot read property 'value' of null 在这种情况下我需要对 value 做什么,请帮助我。 Do I need to assign a value to value我是否需要为 value 赋值

 function makeSearch() { var qty = document.getElementById('qty').value; if (qty > 5) { alert("Maximum 5 toy available.") return false } var cname = document.getElementById('cname').value; var tenure = document.getElementById('tenure').value; var toy = document.getElementById('toy_name').value; var rent = qty * tenure * toy; var answer = window.confirm("Total rental cost = " + rent); if (answer) { alert("Thank you for order Mr/Ms/Mrs: " + cname); window.location.reload(); } else { alert("Order cancelled.") window.location.reload(); } return false } code after return: < tr > < td > Number Of toys available < /td> < td > < input type = "number" name = "avail" id = "avail" value = "" readonly ref = "" required > < /input> < / td > < /tr> < tr >

You are trying to access the value of elements that are not on the page.您正在尝试访问不在页面上的元素的值。

Even if the elements would have been on the page you would bump into issues with code like this, cause var cname = document.getElementById('cname').value;即使元素已经在页面上,您也会遇到这样的代码问题,因为var cname = document.getElementById('cname').value; will run before the elements are mounted to the page.将在元素安装到页面之前运行。

Another issue is that in react you should not be using document.getElementById to get the value out of it.另一个问题是,在反应中,您不应该使用document.getElementById来从中获取价值。

The correct way would be to use the react useState hook react hooks正确的方法是使用react useState hook react hooks

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

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