1

我有一个使用 HTML、PHP 和 jQuery 的简单搜索表单,但即使某些字段为空并且验证器警告出现片刻,表单也会提交。所以我会提交表单,出现关于“必填字段”的警告,并且无论如何都会提交。验证器被触发但没有停止表单。

HTML + PHP

<form action="dater.php" method="post" id="daterange">
<table width = "1000px">
<col width="250px" />
<col width="250px" />
<col width="250px" />
<col width="250px" />
<tr>
<? dateRangeView(); ?>
<td><input class="comment" type="text" name="groups" id="groupname"  placeholder="Enter group name"> *</td>
<td><input class="comment" type="text" name="employee" id="staffname" placeholder="Enter employee name"> *</td>
</tr></table>

*Leave these blank to view all<br>
<button class="mainButton" id="viewTimesheets" value="viewTimesheets" type="submit">View</button>

function dateRangeView() 
{

$query = "SELECT DISTINCT weekending FROM payroll_ts ORDER BY weeke DESC";
$result = mysql_query($query);
echo'<td><select id="startdate" class="infotable" name="startdate"><option value="">---- Start date ----</option>';
while ($row = mysql_fetch_array($result)) 
{
echo'<option value="'.$row{'weeke'}.'">'.$row{'weeke'}.'</option>';
}
echo'</select><br><td><select id="enddate" class="infotable" name="enddate"> <option value="">---- End date ----</option>';

$query = "SELECT DISTINCT weekending FROM payroll_ts ORDER BY weeke DESC";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) 
{
echo'<option value="'.$row{'weeke'}.'">'.$row{'weeke'}.'</option>';
}

echo'</select><br></td>';

}

验证器插件代码

<script language="javascript" type="text/javascript">

/*-------------Validator-------------*/

$.validator.setDefaults({
submitHandler: function() { alert("submitted!"); 
form.submit();}
});

$(function() {

$("#daterange").validate({
rules: {
startdate: "required",
enddate: "required"
},
messages: {
startdate: "required",
enddate: "required"
}
});
});

</script>

我已经用了四个小时了,用过 .on(),返回 false;onSubmit() 和 e.preventDefault(); 虽然我可能已经得到了那些搞砸的语法......这是我正在使用的插件...... http://jqueryvalidation.org/validate/

4

2 回答 2

0

尝试这个。

$(document).ready(function () {

    $.validator.setDefaults({
        submitHandler: function (form) {
            alert('submitted'); 
            form.submit();
        }
    });

    $('#daterange').validate({ 
        rules: {
            startdate: {
                required: true
            },
            enddate: {
                required: true
            }
        }
    });

});

工作 JsFiddle 示例演示

希望这会有所帮助,谢谢

或者这里是你俩的代码

$(document).ready(function () {

    $.validator.setDefaults({
submitHandler: function() { alert("submitted!"); 
 form.submit();}
});

$(function() {

$("#daterange").validate({
rules: {
startdate: "required",
enddate: "required"
},
messages: {
startdate: "required",
enddate: "required"
}
});
});

});
于 2013-08-15T13:08:45.240 回答
0
$(document).ready(function () {

    $("#daterange").validate({
    rules: {
    startdate: "required",
    enddate: "required"
    },
    messages: {
    startdate: "required",
    enddate: "required"
    }
   });

if($("#daterange").valid()) {
    // call your function;
    alert("submitted!");
}

});

于 2013-08-17T19:02:23.417 回答