1
<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 等值......警报(提名朋友)永远不会被执行

4

2 回答 2

1

您可以通过多种方式实现这一目标,

var element = $('#'+qNum);

你可以使用 jQuery 来做到这一点:

if (element.is('input')) {
    // do this
} else if (element.is('select')) {
    // do this
}

或者

if (element.tagName == "INPUT") {
    // do this
} else if (element.tagName == "SELECT") {
    // do this
}

或者

  if(element.prop('type') == 'select-one' ) { 
     // do this
  }
于 2013-08-08T04:35:15.280 回答
1

我相信正确的语法是:

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

没有:

于 2013-08-08T03:57:12.800 回答