如何触发最终显示的文本?

时间:2016-02-07 12:59:29

标签: javascript html

还会有许多其他问题,例如下面的问题。但问题是我希望每个问题都会创建一个文本。例如,如果您单击第一个问题的“是”按钮,将会创建一个文本,例如“您单击第一个问题的是按钮。”

到目前为止它是如此但我怎样才能触发所有问题后出现的文本?

"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>                                                                                                                      

2 个答案:

答案 0 :(得分:1)

一种方法是跳过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>

答案 1 :(得分:0)

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

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