Javascript表单提交错误

时间:2013-10-10 23:55:46

标签: javascript html

提交表单时我的代码出现问题。如果在文本框中没有输入任何内容,则会出现正确的错误,但是当单选按钮响应有有效条目时,它仍会显示错误“请选择一个分数”,例如选择了分数时。

我似乎无法看到我输错了什么。

一切顺利

CP

<form name="promoForm2" method=post enctype=multipart/form-data action=reactsubmit.php onSubmit="return validateForm();">
<ul class=mainForm id="mainForm_1">

<SCRIPT type=text/javascript>

function validateForm()
{
    var x=document.forms["promoForm2"]["DJcomment"].value; 
    if (x==null || x=="") 
    { 
        alert("Please enter a comment."); 
        return false; 
    }  

    var x=document.forms["promoForm2"]["score"].value; 
    if (x==null || x=="") 
    { 
        alert("Please enter a score for the track."); 
        return false; 
    } 

    var x=document.forms["promoForm2"]["FavMix"].value; 
    if (x==null || x=="") 
    { 
        alert("Please select your favourite mix."); 
        return false; 
    } 
}
</SCRIPT>

<table border='0'><tr><td>Support: </td><td><input type="radio" name="DJsupport" value="Yes">Yes<input type="radio" name="DJsupport" value="No">No</td></tr>

<tr><td>Favourite Mix: </td><td><input type="radio" name="FavMix" value="Enemy (Original Mix)">Enemy (Original Mix)</td></tr>

<tr><td></td><td><input type="radio" name="FavMix" value="Enemy (Original Mix)">Enemy (Original Mix)</td></tr>

<tr><td></td></tr><tr><td>Score: </td><td><input type="radio" name="score" value="1">1<input type="radio" name="score" value="2">2<input type="radio" name="score" value="3">3<input type="radio" name="score" value="4">4<input type="radio" name="score" value="5">5<input type="radio" name="score" value="6">6<input type="radio" name="score" value="7">7<input type="radio" name="score" value="8">8<input type="radio" name="score" value="9">9<input type="radio" name="score" value="10">10<td></tr><tr><td>Comment: (Required) </td><td><textarea name="DJcomment" rows="5" cols="40"></textarea></td></tr>

<tr><td></td><td><p class="mainForm"><input id="saveForm" class="mainForm" type="submit" value="Submit Reaction" /></td></tr></li></form>

</html>

2 个答案:

答案 0 :(得分:0)

document.forms["promoForm2"]["score"]是一个输入元素数组,您需要迭代以检查是否有任何一个被检查。

var x=document.forms["promoForm2"]["FavMix"]; 
var pass=false;
for(var i=0;i<x.length;i++){
    if (x[i].checked==true) { 
        pass=true;
        break;
    } 
}
if(pass==false){
    alert("Please select your favourite mix."); 
    return false;
}

查看我的JSFiddle

答案 1 :(得分:0)

要检查单选按钮的值,请检查其checked属性。

您需要检查某个名称的所有单选按钮,以确定是否为其组选择了一个。

function checkRadios(group) {
    for (var i = 0; i < group.length; i++) {
        if (group[i].checked) {
            return true;
        }
    }

    return false;
}

function validateForm() {
    var x=document.forms["promoForm2"]["DJcomment"].value;

    if (x == null || x == "") {
        alert("Please enter a comment.");
        return false;
    }

    if (!checkRadios(document.forms["promoForm2"]["score"])) {
        alert("Please enter a score for the track.");
        return false;
    }

    if (!checkRadios(document.forms["promoForm2"]["FavMix"])) {
        alert("Please select your favourite mix.");
        return false;
    }
}

现场演示here