[英]Writing JS to Store 10 Values But TypeError: input is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML
i Am Writing A JavaScript Function In React For Adding 10 Values To Array This 10 Values To Be Stored From Html Input Field So what i did is added input field with id 'tsk ' and button 'bt3' so whenever the button get hit it run a js script which takes the value of input field 'tsk' and pushed it to empty array in js so every time i hit button the value inside field is pushed to array and prompt over label 'list ' by using innerHTML method i Am Writing A JavaScript Function In React For Adding 10 Values To Array This 10 Values To Be Stored From Html Input Field So what i did is added input field with id 'tsk ' and button 'bt3' so whenever the button get hit it run一个 js 脚本,它获取输入字段 'tsk' 的值并将其推送到 js 中的空数组,因此每次我点击按钮时,字段内的值都会被推送到数组并通过使用 innerHTML 方法提示 label 'list'
here my efforts of html is这里我的 html 的努力是
<div id='array'>
<input type='number' id='tsk'>push 10 values</input>
<label htmlFor='list' id='list' >List values are:</label>
<button id='bt3' onClick={tsk3}>ADD Value</button>
</div>
and here is corresponding js to run it这是相应的js来运行它
function tsk3()
{
let item = document.getElementById('tsk').value;
var array =[1,];
array.push(item);
document.getElementById('list').innerHTML=array;
}
but instead of working the code it showing me unnecessary type error OUTPUT OF WEBPAGE但它没有运行代码,而是向我显示了不必要的类型错误OUTPUT OF WEBPAGE
This error occurs because in React when void elements have either children or dangerouslySetInnerHTML
prop.发生此错误是因为在 React 中,当 void 元素具有子元素或dangerouslySetInnerHTML
的 SetInnerHTML 属性时。 HTML elements such as are void elements that are only self-closing without any content. HTML 元素,例如 void 元素,它们只会自动关闭而没有任何内容。 Therefore, React will throw an exception if you set either children or the innerHTML property for a void element.因此,如果您为 void 元素设置 children 或 innerHTML 属性,React 将抛出异常。 Also, there are syntax errors in your code.此外,您的代码中存在语法错误。 The htmlFor property sets or returns the value of the for attribute of a label. htmlFor 属性设置或返回 label 的 for 属性的值。 The htmlFor attribute specifies which form element a label is bound to which in your case should be 'tsk' ie the input element. htmlFor 属性指定 label 绑定到哪个表单元素,在您的情况下应该是“tsk”,即输入元素。 Accordingly, the label tag should appear before the input tag, something like the following -因此,label 标签应该出现在输入标签之前,如下所示 -
<div id='array'> <label htmlFor='task' id='list' >List values are:</label> <input type='number' id='tsk' placeholder='push 10 values'/> <button id='bt3' onClick={tsk3}>ADD Value</button> </div>
Also note input has to be a self-closing tag.另请注意,输入必须是自闭合标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.