我怎样才能触发一个将在最后显示的文本

How can I trigger a text which will be showed up at the end?

本文关键字:一个 最后 文本 显示      更新时间:2023-09-26

还有许多其他问题,如下所示。但问题是我希望每个问题都能创建一个文本。例如,如果您单击第一个问题的"是"按钮,则会创建一个文本,例如" 您单击了第一个问题的"是"按钮。

到目前为止,它很好,但是我如何还可以触发一个在所有问题之后都会显示的文本?

"There will be first question."
<form>
    <input type="radio" value="yes" onClick="location.href='question triggered by yes'">
    <input type="radio" value="no" onClick="location.href='question triggered by no'">
</form>                                                                                                                      

一种方法是跳过javascript,简单地使用 CSS 伪类:checked

input ~ p {
    display:none;
}
.question1Yes:checked ~ .question1YesRecorded,
.question1No:checked ~ .question1NoRecorded {
    display:block;
}
<form>
<p>1. Your first question - will you answer Yes or No?</p>
<input type="hidden" name="question1" value="question1Unanswered">
<input type="radio" name="question1" class="question1Yes" value="question1Yes">Yes
<input type="radio" name="question1" class="question1No" value="question1No">No
<p class="question1YesRecorded">You answered <strong>Yes</strong></p>
<p class="question1NoRecorded">You answered <strong>No</strong></p>
</form>

不确定你需要什么,但这里有一个函数可以为你创建一些文本:

function create_text(answer) {
    if(answer === "yes")  return "some text";
    if(answer === "no") return "some different text";
}