<FORM NAME="form1" METHOD="POST" ACTION="survey.php">
    <p>q2: Who is your best best friend?</P>
    <select name='q2' id='q21'>
        <option value='0'>Select a Name</option>
        <option value='2001'>Ahmed Ebaid</option>
        <option value='2002'>Jaida ElTayeby</option>
        <option value='2003'>Farida Ebaid</option>
        <option value='2005'>Kenny Andersen</option>
        <option value='2006'>Nadine Saad</option>
        <option value='2007'>AbdElHai Ebaid</option>
        <option value='2008'>Salwa AbdElAal</option>
    </select>
    <select name='q2' id='q22'>
        <option value='0'>Select a Name</option>
        <option value='2001'>Ahmed Ebaid</option>
        <option value='2002'>Jaida ElTayeby</option>
        <option value='2003'>Farida Ebaid</option>
        <option value='2005'>Kenny Andersen</option>
        <option value='2006'>Nadine Saad</option>
        <option value='2007'>AbdElHai Ebaid</option>
        <option value='2008'>Salwa AbdElAal</option>
    </select>
    <P>q3: How do you rate AbdElHai Ebaid?</P>
    <P>
        <INPUT TYPE='Radio' Name='q3' value='1' >1</P>
    <P>
        <INPUT TYPE='Radio' Name='q3' value='2' >2</P>
    <P>
        <INPUT TYPE='Radio' Name='q3' value='3' >3</P>
    <P>
        <INPUT TYPE='Radio' Name='q3' value='4' >4</P>
    <P>
        <INPUT TYPE='Radio' Name='q3' value='5' >5</P><span style='color:red' id='radio_error'></span>
    <p>q4: Who is your best best friend?</P>
    <select name='q4' id='q41'>
        <option value='0'>Select a Name</option>
        <option value='2001'>Ahmed Ebaid</option>
        <option value='2002'>Jaida ElTayeby</option>
        <option value='2003'>Farida Ebaid</option>
        <option value='2005'>Kenny Andersen</option>
        <option value='2006'>Nadine Saad</option>
        <option value='2007'>AbdElHai Ebaid</option>
        <option value='2008'>Salwa AbdElAal</option>
    </select>
    <select name='q4' id='q42'>
        <option value='0'>Select a Name</option>
        <option value='2001'>Ahmed Ebaid</option>
        <option value='2002'>Jaida ElTayeby</option>
        <option value='2003'>Farida Ebaid</option>
        <option value='2005'>Kenny Andersen</option>
        <option value='2006'>Nadine Saad</option>
        <option value='2007'>AbdElHai Ebaid</option>
        <option value='2008'>Salwa AbdElAal</option>
    </select>

    <p>
        <INPUT TYPE="Submit" Name="Submit1" VALUE="Click here to vote">
    </P>
</form>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    <script>
if ($('#'+qNum+'').is(':radio')) {
                if (($('input[name='+qNum+']:checked').length == 0) ){
                    alert("No Selection is made for "+ qNum);
                    return false;
                }
            }
    else if ($('[name="' + qNum + '"]').is(':radio')) {
                    var j = 1;
                    var nominatefriend = qNum+j;
                                    alert (nominatefriend);
    }
    </script


我在这里要做的是基于JavaScript代码验证表单元素的类型,它已经包含在遍历所有问题的for循环中。我不知道基于name属性检查表单元素是否为select的正确语法。在我的情况下,qNum是一个变量,其值如q1,q2,.....
警报(提名朋友)永远不会执行

最佳答案

我相信正确的语法是:

$('[name="' + qNum + '"]').is('select')


没有:

关于javascript - 使用其name属性找出表单元素是否为select类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18117599/

10-16 21:58