这是代码,而不是整个代码,只是其中的一小部分。 我需要一个代码片段来验证选择框。其他字段的验证工作正常。 真的很感谢你的帮助......
<label>Locality :</label><select name="locality" class="locality">
<option value="default" id="select" class="required">--Select locality--</option>';
//<option selected="selected"></option>
$query2 = "Select * from locality_master";
$res = mysqli_query($conn, $query2) or die("Error in Selecting select box locality master " . mysqli_error($conn));
while($r=mysqli_fetch_array($res)){
echo "<option value='" . $r['l_id'] ."'>" . $r['l_name'] ."</option>";
}
echo "</select>";
print '
<label>Street :</label> <select name="street" class="street" >
<option selected="selected">--Select Street--</option>
</select>
<img id="loding1"></img>
<br><br>
现有的Jquery验证
$(function() {
// Setup form validation on the #register-form element
$("#register-form").validate({
// Specify the validation rules
rules: {
name: "required",
address: "required",
state: "required",
city: "required",
postalcode: "required",
// Specify the validation error messages
messages: {
name: "Please enter your first name",
address: "Please enter your address:",
state: "Please provide a state:",
city: "Please provide a city:",
postalcode: "Please Provide us with a postal code"
},
submitHandler: function(form) {
form.submit();
}
}
});
});
我应该向Jquery添加什么来验证选择框?
答案 0 :(得分:2)
在选择地点的选项中留空值
<option value="" id="select" class="required">--Select locality--</option>
并提出规则和消息
// Setup form validation on the #register-form element
$("#register-form").validate({
// Specify the validation rules
rules: {
name: "required",
address: "required",
state: "required",
city: "required",
postalcode: "required",
locality : "required"
},
// Specify the validation error messages
messages: {
name: "Please enter your first name",
address: "Please enter your address:",
state: "Please provide a state:",
city: "Please provide a city:",
postalcode: "Please Provide us with a postal code",
locality : "Please Select Locality."
},
submitHandler: function(form)
{
form.submit();
}
});
答案 1 :(得分:0)
在提交处理程序
中执行此操作 if($('.locality option:selected').text() == '--Select locality--'){
return false;
// and alert message
}
答案 2 :(得分:0)
为您的选项标记添加值属性,如下所示:
<option value="" selected="selected">--Select Street--</option>
然后将选择框添加到验证中,如下所示:
// Specify the validation rules
rules: {
name: "required",
address: "required",
state: "required",
city: "required",
postalcode: "required",
street: "required"
// Specify the validation error messages
messages: {
name: "Please enter your first name",
address: "Please enter your address:",
state: "Please provide a state:",
city: "Please provide a city:",
postalcode: "Please Provide us with a postal code",
street: "Please provide a street"
},