好,所以我正在制作一个允许用户构建测验(如嗡嗡声测验)的表格,但我不想限制用户他们必须拥有的问询数量。所以我想我可以做些什么,在问题的底部有一个“添加问题”按钮,使用户可以在所有适当的字段中添加一个问题。现在,我只是在表单部分上工作,我已经设置了逻辑来构成测验功能和内容,但是所有测验详细信息(答案,问题等)都经过了硬编码。我也不怎么了解这些事情(html,js,react等),因此,如果还有其他看起来很时髦的地方,我欢迎您提出一些建议。反正...这是我的代码。
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
var questionNum = 1;
class App extends Component {
render() {
return (
<div className="App">
<div>
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Welcome to React</h1>
</header>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
</div>
<form>
Give your quiz a title:<br />
<input type="text" name="quizTitle" /><br />
Who's the author?<br />
<input type="text" name="author" /><br />
Now let's add some questions...<br />
<form className="quesitons">
<div id={"questions" + questionNum}>
Question {questionNum}<br />
<input type="text" name={"question" + questionNum}/><br />
Answers<br />
Check the box(es) for the correct answer(s).<br />
<input type="checkbox" name={"question" + questionNum + "Answer1Box"} />
<label for={"question" + questionNum + "Answer1"}><input type="text" name={"question" + questionNum + "Answer1"}/></label><br />
<input type="checkbox" name={"question" + questionNum + "Answer2Box"} />
<label for={"question" + questionNum + "Answer2"}><input type="text" name={"question" + questionNum + "Answer2"}/></label><br />
<input type="checkbox" name={"question" + questionNum + "Answer3Box"} />
<label for={"question" + questionNum + "Answer3"}><input type="text" name={"question" + questionNum + "Answer3"}/></label><br />
<input type="checkbox" name={"question" + questionNum + "Answer4Box"} />
<label for={"question" + questionNum + "Answer4"}><input type="text" name={"question" + questionNum + "Answer4"}/></label><br />
</div>
<div id="container"></div>
</form>
</form>
<button id="addQuestionButton" onClick='addQuestion ()'>Add Question</button>
</div>
);
}
}
function addQuestion () {
questionNum++;
console.log(questionNum + "that is the question number");
var div = document.createElement('div');
div.className = '{"questions" + questionNum}';
div.innerHTML =
'Question {questionNum}<br />\
<input type="text" name={"question" + questionNum}/><br />\
Answers<br />\
Check the box(es) for the correct answer(s).<br />\
<input type="checkbox" name={"question" + questionNum + "Answer1Box"} />\
<label for={"question" + questionNum + "Answer1"}><input type="text" name={"question" + questionNum + "Answer1"}/></label><br />\
<input type="checkbox" name={"question" + questionNum + "Answer2Box"} />\
<label for={"question" + questionNum + "Answer2"}><input type="text" name={"question" + questionNum + "Answer2"}/></label><br />\
<input type="checkbox" name={"question" + questionNum + "Answer3Box"} />\
<label for={"question" + questionNum + "Answer3"}><input type="text" name={"question" + questionNum + "Answer3"}/></label><br />\
<input type="checkbox" name={"question" + questionNum + "Answer4Box"} />\
<label for={"question" + questionNum + "Answer4"}><input type="text" name={"question" + questionNum + "Answer4"}/></label><br />\
';
document.getElementsByClassName('questions').appendChild(div);
}
export default App;
(我知道我的页面上有来自React的东西,这不是我的,但我认为它现在很可爱,所以我要把它留在那里直到我使此表单起作用为止) “ addQuestion”按钮...
invariant.js:42 Uncaught Error: Expected `onClick` listener to be a function, instead got a value of `string` type.
这不是全部错误(我将其放在最后),但是我不确定如何解决此问题。我也不太明白这是什么意思,所以如果有人可以解释一下,那就好了。
这是完整的错误(哦,我正在使用开发工具在Google Chrome浏览器中收到此错误,但您可能已经知道了)
Uncaught Error: Expected `onClick` listener to be a function, instead got a value of `string` type.
at invariant (invariant.js:42)
at getListener (react-dom.development.js:680)
at listenerAtPhase (react-dom.development.js:981)
at accumulateDirectionalDispatches (react-dom.development.js:1004)
at traverseTwoPhase (react-dom.development.js:924)
at accumulateTwoPhaseDispatchesSingle (react-dom.development.js:1020)
at forEachAccumulated (react-dom.development.js:562)
at accumulateTwoPhaseDispatches (react-dom.development.js:1063)
at Object.extractEvents (react-dom.development.js:4396)
at extractEvents (react-dom.development.js:697)
at runExtractedEventsInBatch (react-dom.development.js:731)
at handleTopLevel (react-dom.development.js:4476)
at batchedUpdates$1 (react-dom.development.js:16659)
at batchedUpdates (react-dom.development.js:2131)
at dispatchEvent (react-dom.development.js:4555)
at interactiveUpdates$1 (react-dom.development.js:16714)
at interactiveUpdates (react-dom.development.js:2150)
at dispatchInteractiveEvent (react-dom.development.js:4532)
谢谢!
答案 0 :(得分:1)
btid
应该改为onClick='addQuestion ()'
这样做的原因是JSX,尽管它看起来像HTML,但实际上不是HTML,因此存在一些细微的差异。