我有一个从数据库填充的选择选项字段,我想验证用户何时没有选择任何内容并按下提交按钮。我无法获得验证。这是我到目前为止所做的:
对于选择选项列表:
<form id="dataForm" method="get" action="">
<b>Year</b>
<?php
$sql = 'SELECT Year_Id,Year_Released FROM {extdb_year} WHERE Only_For_Trend = 0 ORDER BY Year_Released DESC';
$res = db_query($sql);
echo "<select id='Year_Released' name='Year_Released'>
<option value='-1'>-Select-</option>";
$cnt = 0;
$options = array();
foreach ($res as $row)
{
$cnt++;
echo "<option value = '{$row->Year_Id}'";
if ($cnt==1)
echo "selected = 'selected'";
echo ">{$row->Year_Released}</option>";
$options[$row->Year_Id] = $row->Year_Released;
}
echo "</select>";
?>
<p>
<input class="submit" type="submit" value="Submit"/>
</p>
</form>
用于验证的 JQuery 代码
$(document).ready(function(){
$("#dataForm").validate({
rules: {
Year_Released: {
required: true
}
},
messages: {
Year_Released: "You must select a Year."
}
});
});
在这方面的任何建议都将受到高度赞赏。