【问题标题】:How to make retrieving the value from the button once it has been clicked more efficient单击按钮后如何更有效地从按钮中检索值
【发布时间】:2023-02-05 21:04:32
【问题描述】:

基本上我在做一种问卷调查,其中列出了问题,你可以用按钮表达你的选择,从“不感兴趣”到“非常重要”(分别从“1”到“4”)

我使用的方法是这样的,虽然它似乎不是最好/最有效的方法:

<div className="mt-4 d-grid gap-2 col-8 mx-auto">
    <button className="btn btn-light shadow-sm btn-lg fw-normal" type="submit" value="1" onClick={e => handleAnswers(e, "value")}>Molto poco</button>
    <button className="btn btn-light shadow-sm btn-lg fw-normal" type="submit" value="2" onClick={e => handleAnswers(e, "value")}>Abbastanza</button>
    <button className="btn btn-light shadow-sm btn-lg fw-normal" type="submit" value="3" onClick={e => handleAnswers(e, "value")}>Importanti</button>
    <button className="btn btn-light shadow-sm btn-lg fw-normal" type="submit" value="4" onClick={e => handleAnswers(e, "value")}>Fondamentali</button>
</div>

这是被调用的函数:

    const handleAnswers = (e) => {
        e.preventDefault();
        let questionResponse = e.target.value; //=====> This is where i get the answere value
        let questionId = currentQuestionCounter;

        setAnswers([...answers, { questionId, questionResponse }]);
        nextQuestion();
    }

有没有办法以更美观/更实用的方式实现相同的结果?

【问题讨论】:

  • {["Molto poco", ...].map((label, index) =&gt; ...)}
  • 完成了,很好的建议!

标签: reactjs


【解决方案1】:

我认为您的方法是最容易理解的方法。即使你稍后再看它。

【讨论】:

    猜你喜欢
    • 2021-12-08
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-18
    • 1970-01-01
    相关资源
    最近更新 更多