0
<FORM NAME="form1" METHOD="POST" ACTION="survey.php">
    <P>q1: How do you rate Ahmed Ebaid?</P>
    <P>
        <INPUT TYPE='Radio' Name='q1' value='1' id='q1'>1</P>
    <P>
        <INPUT TYPE='Radio' Name='q1' value='2' id='q1'>2</P>
    <P>
        <INPUT TYPE='Radio' Name='q1' value='3' id='q1'>3</P>
    <P>
        <INPUT TYPE='Radio' Name='q1' value='4' id='q1'>4</P>
    <P>
        <INPUT TYPE='Radio' Name='q1' value='5' id='q1'>5</P><span style='color:red' id='radio_error'></span>
    <p>q2: Who is your best best friend?</P>
    <select name='q21' id='q2'>
        <option value='0'>Select a Name</option>
        <option value='2001'>abc</option>
        <option value='2002'>def</option>
        <option value='2003'>ghi</option>
        <option value='2005'>jkl</option>
        <option value='2006'>mno</option>
        <option value='2007'>pqr</option>
        <option value='2008'>stu</option>
    </select>
    <select name='q22' id='q2'>
        <option value='0'>Select a Name</option>
        <option value='2001'>abc</option>
        <option value='2002'>def</option>
        <option value='2003'>ghi</option>
        <option value='2005'>jkl</option>
        <option value='2006'>mno</option>
        <option value='2007'>pqr</option>
        <option value='2008'>stu</option>
    </select>
    <select name='q23' id='q2'>
        <option value='0'>Select a Name</option>
        <option value='2001'>abc</option>
        <option value='2002'>def</option>
        <option value='2003'>ghi</option>
        <option value='2005'>jkl</option>
        <option value='2006'>mno</option>
        <option value='2007'>pqr</option>
        <option value='2008'>stu</option>
    </select>
    <P>q3: How do you rate def?</P>
    <P>
        <INPUT TYPE='Radio' Name='q3' value='1' id='q3'>1</P>
    <P>
        <INPUT TYPE='Radio' Name='q3' value='2' id='q3'>2</P>
    <P>
        <INPUT TYPE='Radio' Name='q3' value='3' id='q3'>3</P>
    <P>
        <INPUT TYPE='Radio' Name='q3' value='4' id='q3'>4</P>
    <P>
        <INPUT TYPE='Radio' Name='q3' value='5' id='q3'>5</P><span style='color:red' id='radio_error'></span>
    <p>q4: Who is your best best friend?</P>
    <select name='q41' id='q4'>
        <option value='0'>Select a Name</option>
        <option value='2001'>abc</option>
        <option value='2002'>def</option>
        <option value='2003'>ghi</option>
        <option value='2005'>jkl</option>
        <option value='2006'>mno</option>
        <option value='2007'>pqr</option>
        <option value='2008'>stu</option>
    </select>
    <select name='q42' id='q4'>
        <option value='0'>Select a Name</option>
        <option value='2001'>abc</option>
        <option value='2002'>def</option>
        <option value='2003'>ghi</option>
        <option value='2005'>jkl</option>
        <option value='2006'>mno</option>
        <option value='2007'>pqr</option>
        <option value='2008'>stu</option>
    </select>
    <select name='q43' id='q4'>
        <option value='0'>Select a Name</option>
        <option value='2001'>abc</option>
        <option value='2002'>def</option>
        <option value='2003'>ghi</option>
        <option value='2005'>jkl</option>
        <option value='2006'>mno</option>
        <option value='2007'>pqr</option>
        <option value='2008'>stu</option>
    </select>
    <select name='q44' id='q4'>
        <option value='0'>Select a Name</option>
        <option value='2001'>abc</option>
        <option value='2002'>def</option>
        <option value='2003'>ghi</option>
        <option value='2005'>jkl</option>
        <option value='2006'>mno</option>
        <option value='2007'>pqr</option>
        <option value='2008'>stu</option>
    </select>
    <P>q5: How do you rate the attitude of Farida Ebaid?</P>
    <P>
        <INPUT TYPE='Radio' Name='q5' value='1' id='q5'>1</P>
    <P>
        <INPUT TYPE='Radio' Name='q5' value='2' id='q5'>2</P>
    <P>
        <INPUT TYPE='Radio' Name='q5' value='3' id='q5'>3</P>
    <P></P>
    <P></P><span style='color:red' id='radio_error'></span>
    <p>
        <INPUT TYPE='hidden' Name='h2' VALUE=''>
    </P>
    <p>
        <INPUT TYPE="Submit" Name="Submit1" VALUE="Click here to vote">
    </P>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    <script>
        $(function(){
                $('form').submit(function(e){
                    var answers = '';
            var len = 5;
                    $('input[type=Radio]:checked').each(function() {
                        if (answers !== '') {
                            answers += ',';
                        }
                        answers += $(this).val();
                //alert(answers);
                    })
                    $('input[name=h2]').val(answers);       
            for (var i = 1; i <= len; i++ ) {
                var qNum = 'q'+i;
                //document.write(qNum);
                if ($('#'+qNum+'').is(':radio')) {
                    if (($('input[name='+qNum+']:checked').length == 0) ){
                        alert("No Selection is made for "+ qNum);
                        return false;
                    }
                }
                else if ($('#'+qNum+'').is('select')) {
                    var j = 1;
                    var nominatefriend = qNum+j;
                    alert (nominatefriend);
                    var check = '';
                    var chosen = new Array();
                    while (check = document.getElementsByName("nominatefriend")) {
                        alert(check);
                        var len = document.form1[nominatefriend].length;
                        alert (len);
                        for (var i = 0; i < len; i++) {
                            if (document.form1[nominatefriend][i].selected) {
                                chosen[j] = document.form1[nominatefriend][i].value;
                                alert (chosen[j]);
                            }
                        }
                        j++;
                        nominatefriend = qNum+j;
                        alert (nominatefriend);
                    }
                }
            }   // for loop        
                }); // form submit function
        })//function

上面的代码执行得很好。但是,对于不存在的表单元素 q24,它会报告以下错误。类型错误:document.form1[nominatefriend] 未定义。这又是一个不存在的表单元素。我在想,一旦给定的表单元素不存在,while 循环就应该退出。

4

2 回答 2

1

Ok, I've reworked this to use JQuery to look at each element rather than trying to find the names of each element using a loop. I feel this is a more concise way to do it.

http://jsfiddle.net/SeanKendle/9VHF3/

check out the fiddle answer... let me know if you need more help.
于 2013-08-07T21:21:54.300 回答
0

去掉周围的引号nominateFriend

while (check = document.getElementsByName(nominatefriend)) {

另外,我建议也删除作业

while (document.getElementsByName(nominatefriend)) {
于 2013-08-07T21:25:45.100 回答