mardi 26 octobre 2021

Why last checked checkbox keep it when i go to next questions? ReactJs

i am working on survey questioner and where i have multiple questions and when i check it one question checkbox it keep checked also next questions and so on , so how to now keep check when i go to next question on survey ?

here is json list of questions. Please help me i am struggling fro last few days

Here is what i did not far ?. This is checked function

const processInput = (e) => {
    const checked = e.target.checked;
    let detachedAnswers = answers;
    if (checked) {
      let index = detachedAnswers.findIndex((i) => i.question === show.id);
      // detachedAnswers[index] = e.target.checked;
      detachedAnswers[index].body.push(e.target.value);

      console.log(detachedAnswers);
      setAnswers(detachedAnswers);
    } else {
      const filtered = detachedAnswers[number].body.filter(
        (string) => string !== e.target.value
      );
      detachedAnswers[number].body = filtered;
      setAnswers(detachedAnswers);
    }
  };

here is the survey display using checkbox.

<div className="survey_option">
                {show?.choices?.split(",").map((c, index) => (
                  <div className="fix_lable" key={index + 1}>
                    <input
                      type="checkbox"
                      name="body"
                      id={c}
                      value={c}
                      // checked
                      // checked={c}
                      onChange={(e) => processInput(e, show.id)}
                    />
                    <label id={c} htmlFor={c}>
                      {c.split("-")[0]}
                    </label>
                  </div>
                ))}
              </div>
            </div>



Aucun commentaire:

Enregistrer un commentaire