我正在使用一个名为steps的 jQuery 插件来创建表单向导。可以与 jQuery验证插件一起使用。
在用户继续下一组选项之前,我需要能够验证选择列表,但我还没有找到这样做的方法。
到目前为止,这是我的代码。的HTML
<h2>Engagement</h2>
<div id = "pane1">
<div class = "panel panel-default">
<div class = "panel-heading">
Engagement
</div>
<div class = "panel-body">
<table class='table table-hover'>
<tr valign='top'><td class='tableLabel'>Request Type:</td><td><select onchange = "getType();" id = "type" class="selectpicker required" name='type'>
<?php
foreach($types AS $key => $val){
echo "<option value='{$key}'>{$val}</option>";
}
?>
</select></td></tr>
<tr valign='top'><td class='tableLabel'>Delivery Date:</td><td><input id='deliveryDate' placeholder="Select a delivery date for your request" class="form-control datePicker required" type='text' name='devlireyDate'></td></tr>
</table>
</div>
</div>
</div>
<h2>Details</h2>
<div id = "pane2">
</div>
</form>
jQuery
$(function (){
$("#engagementForm").steps({
headerTag: "h2",
bodyTag: "div",
transitionEffect: "slideLeft",
onStepChanging: function (event, currentIndex, newIndex)
{
// Always allow going backward even if the current step contains invalid fields!
if (currentIndex > newIndex)
{
return true;
}
var form = $(this);
// Clean up if user went backward before
if (currentIndex < newIndex)
{
// To remove error styles
$(".body:eq(" + newIndex + ") label.error", form).remove();
$(".body:eq(" + newIndex + ") .error", form).removeClass("error");
}
// Disable validation on fields that are disabled or hidden.
form.validate().settings.ignore = ":disabled,:hidden";
// Start validation; Prevent going forward if false
return form.valid();
},
onFinishing: function (event, currentIndex)
{
var form = $(this);
// Disable validation on fields that are disabled.
// At this point it's recommended to do an overall check (mean ignoring only disabled fields)
form.validate().settings.ignore = ":disabled";
// Start validation; Prevent form submission if false
return form.valid();
},
onFinished: function (event, currentIndex)
{
var form = $(this);
// Submit form input
form.submit();
}
});
$('.selectpicker').selectpicker();
$(".datePicker").datepicker({ dateFormat: 'yy-mm-dd' , minDate: "+1d" });
});
交货日期只需通过 puttint 'required' 作为一个类来验证,但遗憾的是,这不适用于选择列表。