0

我有一个 html 表单,我想在单击按钮时将文本从“冠军联赛琐事”更改为“曼彻斯特”。它只改变了一秒钟,然后变回“冠军联赛琐事”。我无法弄清楚为什么它会变回来。有任何想法吗?

<script  type="text/javascript" src="quiz.js" ></script>
<body>
<form action="" method="post" onsubmit="return checkscript()">
    <h1>Choose a Quiz</h1>

    <p><input type="radio" name="Radio" value="1" /><font size="5"
    color="#0033CC" id="cl">Champions League Trivia</font><br />
    <br />
    <input type="radio" name="Radio" value="2" /><font size="5"
    color="#CC0000" id="epl">English Premier League Trivia</font><br />
    <br />
    <input type="radio" name="Radio" value="3" /><font size="5"
    color="#660033" id="ll">La Liga Trivia</font><br />
    <br />
    <input type="submit" name="Submit" value="Go" onclick="quizRun()" /></p>
    </form>
    <p id="result"></p>
    </body>
    </html>

这是javascript

function quizRun()
{
var inputs = document.getElementsByTagName("input");
var choice = 0;
if (inputs[0].checked) {
        choice = 1;
        var cl = document.getElementById('cl');
        cl.textContent = 'Manchester';
}
if (inputs[1].checked) {
        choice = 2;

}
if (inputs[2].checked) {
        choice = 3;
}
}
function checkscript()
{
    var inputs = document.getElementsByTagName("input");
    for (var i=0, l=inputs.length; i<l; i++) {
        if (inputs[i].name === "Radio" && inputs[i].checked) return true;
    }
    return false;
}
4

4 回答 4

1

I tried your script and I found this: the value changes but the form is submitted, so the page reloads and the texts changes back.

于 2013-04-11T10:26:27.557 回答
1

发生的事情是您的按钮是type = "submit". 当您提交表单时,页面正在重新加载并返回原始值。

如果您不想提交表单,只需将“开始”按钮更改为type = "button"

于 2013-04-11T10:29:15.033 回答
0

Try using cl.innerHTML = 'Manchester';

Edit:

Your Form tag has no action so the page probably refreshes causing the original value to return. Adding a return false or preventDefault to your form submit event will prevent the form from being submitted.

于 2013-04-11T10:25:16.557 回答
0

我实际上自己尝试了代码,innerHTML或innerText都不起作用@ben fossen所以这可能发生的唯一原因是因为值实际上发生了变化,但随后表单提交并重置了值

于 2013-04-11T10:30:09.680 回答