在一次单击后禁用提交按钮而不牺牲表单的功能

时间:2013-10-05 08:28:43

标签: javascript php html forms

我之前已经问过这个问题,但没有人能够给出满意的答案。 我有一个测验网站,有四个选项的四个单选按钮。当用户单击提交按钮时,将执行PHP脚本,检查答案是否正确。 我需要做的是在按一次后禁用提交按钮。我在上一个问题中收到的所有答案都干扰了我的表单的功能(它没有被提交)。请提供一个不牺牲表单功能的解决方案。 此外,如果你给出一个Javascript代码作为你的答案,请帮助我实现它,因为我在Javascript中天真。

这是我的代码:

<form action="programming_skills.php?ad=<?php echo $a; ?>" method="post" id="quizsubmit">
        <tr align="center">
        <?php echo $row['q_desc']; ?><br />
        <input type="radio" name="ans" value="<?php echo $row['ans1']; ?>" <?php echo ($_POST['ans'] == $row['ans1'] ? 'checked' : '') ?>/>
        <?php echo $row['ans1']; ?><br />
        <input type="radio" name="ans" value="<?php echo $row['ans2']; ?>" <?php echo ($_POST['ans'] == $row['ans2'] ? 'checked' : '') ?>/>
        <?php echo $row['ans2']; ?><br />
        <input type="radio" name="ans" value="<?php echo $row['ans3']; ?>" <?php echo ($_POST['ans'] == $row['ans3'] ? 'checked' : '') ?>/>
        <?php echo $row['ans3']; ?><br />
        <input type="radio" name="ans" value="<?php echo $row['ans4']; ?>" <?php echo ($_POST['ans'] == $row['ans4'] ? 'checked' : '') ?>/>
        <?php echo $row['ans4']; ?><br />

        <tr><td><input type="submit" id="sub" value=Submit_Answer></td></tr></form></table><table border="1" align="center">

        <?php

                if(isset($_POST['sub']))
            {
                $a_value=$a;
                $answer=$_POST['ans'];
        $q2="select * from question where q_id=$a_value";
        $r2=mysql_query($q2);
                if ($row=mysql_fetch_array($r2))
                $trueans=$row['true_ans'];

                    if ($answer==$trueans)
                {
                    $userid=$_SESSION['user_email'];
                    $q1="select * from temp_score WHERE user_id='$userid'";
                    $rw=mysql_query($q1);
                    while($row=mysql_fetch_assoc($rw))
                    $score=$row['temp_score'];
                    $score=++$score;
                    $z="update temp_score set temp_score='$score' where user_id='$userid'";
                    mysql_query($z);
                        ?>


                        Your answer is correct.     <?php               $a=++$a; ?>

                         &nbsp;Click <a href="programming_skills.php?ad=<?php echo $a; ?>">Here</a> for next question
                        <?php
                    }
                    else
                    {
                        ?>Your answer is wrong. The correct answer is <i>'<?php echo $trueans; ?>'</i>.<br />
        <?php               $a=++$a; ?>

                Click <a href="programming_skills.php?ad=<?php echo $a; ?>">Here</a> for next question
                <?php       }
                }

                        ++$a;
                $a=++$a;

                }
                }
        }
        else
        {
            echo '<meta http-equiv="refresh" content="0; url=results.php">';
            }


            ?>

3 个答案:

答案 0 :(得分:0)

嗨发现了一个小提琴,可能有助于你防止默认点击事件在代码下面有帮助......

document.getElementsById("quizsubmit").onclick = function () {
    this.disabled = true;
};

http://jsfiddle.net/zb8CZ/

答案 1 :(得分:0)

超时后禁用它:

<input type="submit" id="sub" value=Submit_Answer onclick="setTimeout('document.getElementById(\"sub\").disabled=!0;',100);">

答案 2 :(得分:0)

您可以在提交按钮上使用onclick事件,如下所示:

onclick="this.disabled='disabled';this.form.submit();"

因此,表单通过JS提交。

或者更好地利用表单元素上的onsubmit事件:

onsubmit="document.getElementById('submit_button').disabled = 'disabled';"

“submit_button”是这种情况下按钮的ID属性。

相关问题