<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,..... 警报(提名朋友)永远不会被执行
答案 0 :(得分:1)
我相信正确的语法是:
$('[name="' + qNum + '"]').is('select')
没有:
答案 1 :(得分: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
}