簡體   English   中英

JavaScript表單(帶有單選按鈕)驗證不起作用

[英]Javascript form (with radio buttons) validation not working

該代碼今天早些時候可以正常工作,然后被隨機停止,必須進行一些更改,但是我無法解決問題-請參見以下內容:

** UPDATE-下面的代碼更新。 現在,它可以正確識別是否選中了單選按鈕,但是,如果用戶在顯示警報后嘗試重新提交表單,則識別的狀態將無法更新。 是否需要某種循環?

  function checkForm()
{
var x=document.forms["sdcomp"]["name"].value;
if (x==null || x=="")
{
alert("Name must be filled out");
return false;
}
var x=document.forms["sdcomp"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Please enter a valid e-mail address");
return false;
}
if (document.getElementById("questionone").checked==false)
{
alert("Question one must be completed");
return false;
}
if (document.getElementById("questiontwo").checked==false)
{
alert("Question two must be completed");
return false;
}
if (document.getElementById("questionthree").checked==false)
{
alert("Question three must be completed");
return false;
}
if (document.getElementById("questionfour").checked==false)
{
alert("Question four must be completed");
return false;
}
}

表格第一個問題的示例:

<tr>
<td class="question" colspan="4"><p class="question">Usain Bolt is the current world     record holder for the men's 100 meters, but what is his best time?</p></td>
</tr>

<tr>
<td class="answer">8.45 seconds<input type="radio" value="Aone" name="Qone"    id="questionone"></td>
<td class="answer">9.58 seconds<input type="radio" value="Atwo" name="Qone" id="questionone"></td>
<td class="answer">10.12 seconds<input type="radio" value="Athree" name="Qone" id="questionone"></td>
<td class="answer">9.32<input type="radio" value="Afour" name="Qone" id="question one">
   </td>
</tr>

** * ** 更新以下新代碼: ** * ** * ** * ** *

<link rel="stylesheet" href="style.css" type="text/css">
<!-- inclusion of fb config -->

<?php require_once('config.php'); ?>

<!-- client side validation for text fields -->

<script language="JavaScript" type="text/javascript">

function checkForm()
{
var x=document.forms["sdcomp"]["name"].value;
if (x==null || x=="")
{
alert("Name must be filled out");
return false;
}
var x=document.forms["sdcomp"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Please enter a valid e-mail address");
return false;
}
/* HERE */
function isAnswered(name) {
var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
    var input = inputs[i];
    if ((input.name === name) && (input.checked)) {
        return true;
    }
}
return false;
}
if (!isAnswered("Qone")) {
alert("Question one must be completed");
return false;
}
if (!isAnswered("Qtwo")) {
alert("Question two must be completed");
return false;
}
if (!isAnswered("Qthree")) {
alert("Question three must be completed");
return false;
}
if (!isAnswered("Qfour")) {
alert("Question four must be completed");
return false;
}
}
</script>


<?php
$con = mysql_connect("localhost","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("sdfbcomp", $con);

// some code
?>

<?php 
$name = $_POST["name"];
$email = $_POST["email"];
$Qone = $_POST["Qone"];
$Qtwo = $_POST["Qtwo"];
$Qthree = $_POST["Qthree"];
$Qfour = $_POST["Qfour"];
?>
</head>
<body>
<div id="tablediv">
<table id="comptable">

<form name="sdcomp" method="post" action="insert.php" onSubmit="return checkForm();">



<tr class="userdetailfields">
<p>Name:</p><input type="text" size="12" maxlength="12" name="name"></br>
</tr>

<tr class="userdetailfields">
<p>Email:</p><input type="text" size="12" maxlength="30" name="email"></br>
</tr>

<tr>
<td class="question" colspan="4"><p class="question">Usain Bolt is the current world record holder for the men's 100 meters, but what is his best time?</p></td>
</tr>

<tr>
<td class="answer">8.45 seconds<input type="radio" value="Aone" name="Qone" id="questionone"></td>
<td class="answer">9.58 seconds<input type="radio" value="Atwo" name="Qone" id="questionone"></td>
<td class="answer">10.12 seconds<input type="radio" value="Athree" name="Qone" id="questionone"></td>
<td class="answer">9.32<input type="radio" value="Afour" name="Qone" id="question one">    </td>
</tr>

<tr>
<td class="question" colspan="4"><p class="question">Question two:</p></td>
</tr>

<tr>
<td class="answer">Answer one<input type="radio" value="Bone" name="Qtwo" id="questiontwo"></td>
<td class="answer">Answer two<input type="radio" value="Btwo" name="Qtwo" id="questiontwo"></td>
<td class="answer">Answer three<input type="radio" value="Bthree" name="Qtwo" id="questiontwo"></td>
<td class="answer">Answer four<input type="radio" value="Bfour" name="Qtwo" id="questiontwo"></td>
</tr>

<tr>
<td class="question" colspan="4"><p class="question">Question three:</td></p>
</tr>

<tr>
<td class="answer">Answer one<input type="radio" value="Cone" name="Qthree" id="questionthree"></td>
<td class="answer">Answer two<input type="radio" value="Ctwo" name="Qthree" id="questionthree"></td>
<td class="answer">Answer three<input type="radio" value="Cthree" name="Qthree" id="questionthree"></td>
<td class="answer">Answer four<input type="radio" value="Cfour" name="Qthree" id="questionthree"></td>
</tr>

<tr>
<td class="question" colspan="4"><p class="question">Question four:</td></p>
</tr>

<tr>
<td class="answer">Answer one<input type="radio" value="Done" name="Qfour" id="questionfour"></td>
<td class="answer">Answer two<input type="radio" value="Dtwo" name="Qfour" id="questionfour"></td>
<td class="answer">Answer three<input type="radio" value="Dthree" name="Qfour" id="questionfour"></td>
<td class="answer">Answer four<input type="radio" value="Dfour" name="Qfour" id="questionfour"></td>
</tr>

<tr><td><input type="submit" value="submit" name="submit" id="submit"><br /></td></tr>
</form><br />

</table>
</div>
</body>

仍然有一個問題,你能幫我嗎? 再次感謝!

首先,在您的問題中,所有答案都具有相同的id ='questionone'。 您需要刪除這些ID。

要檢查是否選中了特定的名稱組,可以使用:

function isAnswered(name) {
    var inputs = document.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
        var input = inputs[i];
        if ((input.name === name) && (input.checked)) {
            return true;
        }
    }
    return false;
}

然后,您可以使用以下代碼測試每個問題組:

if (!isAnswered("Qone")) {
    alert("Question one must be completed");
    return false;
}

您能顯示整個表格嗎? 同時,請嘗試以下操作:

function checkForm() {
    var x=document.forms["sdcomp"]["name"].value;

    if (x==null || x=="") {
        alert("Name must be filled out");
        return false;
    }

    var x=document.forms["sdcomp"]["email"].value;
    var atpos=x.indexOf("@");
    var dotpos=x.lastIndexOf(".");

    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
        alert("Please enter a valid e-mail address");
        return false;
    }

    if (document.getElementById("questionone").checked) {

        alert("Option one is selected");

    } else if (document.getElementById("questiontwo").checked) {

        alert("Option two is selected");

    } else if (document.getElementById("questionthree").checked) {

        alert("Option three is selected");

    } else if (document.getElementById("questionfour").checked) {

        alert("Option four is selected");

    } else {

        alert("No option is selected");

    }
}

這就是我想要告訴您的,在您的輸入上放置唯一的ID以在函數中獲取其值:

<tr>
    <td class="answer">
        <label>8.45 seconds</label>
        <input type="radio" value="Aone" name="Qone" id="questionone" />
    </td>
    <td class="answer">
        <label>9.58 seconds</label>
        <input type="radio" value="Atwo" name="Qtwo" id="questiontwo" />
    </td>
    <td class="answer">
        <label>10.12 seconds</label>
        <input type="radio" value="Athree" name="Qthree" id="questionthree" />
    </td>
    <td class="answer">
        <label>9.32 seconds</label>
        <input type="radio" value="Afour" name="Qfour" id="questionfour" />
    </td>
</tr>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM