html是
$(":input.validate-required").on('keyup', function() {
$(this).closest(".form-group").removeClass("has-error");
$(this).next('small').addClass('hide');
});
$("#room_number").on('keyup', function() {
if (validateRoomNumber() == false) {
$(this).closest(".form-group").addClass("has-error");
$("#duplicate_error").removeVlass('hide');
}
});
function validateRoomNumber() {
var FloorNumber = $("#floor_number").val().trim();
var i;
for (i = 0; i < arrayOfAssignedRoomNumber.length; i++) {
var AssignedFloorNumber = arrayOfAssignedRoomNumber[i];
if (AssignedFloorNumber == FloorNumber) {
return false;
}
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="form-group required">
<label class="col-sm-3 control-label">Room Number</label>
<div class="col-sm-4">
<input id="room_number" name="room_number" type="number" class="form-control validate-required" min="0" value="" />
<small id="room_number_help" class="text-danger hide">Room Number is Required</small>
<small id="duplicate_error" class="text-danger hide">Room Number is Already Assigned</small>
</div>
</div>
我在下面写了一行代码,用于在javascript中进行验证。
请注意,以下行也很有用
$(":input.validate-required").on('keyup', function () {
$(this).closest(".form-group").removeClass("has-error");
$(this).next('small').addClass('hide');
});
现在当用户输入任何已存在的号码时,不会显示重复的错误消息。
请注意,arrayOfAssignedRoomNumber包含数据库中现有的房间号。
请帮助!!!
答案 0 :(得分:1)
很少快速,将其更改为.nextAll()
,以便同时针对两者:
$(this).nextAll('small').addClass('hide');
将函数validateRoomNumber()
更改为:
function validateRoomNumber() {
var FloorNumber = $("#floor_number").val().trim();
if (arrayOfAssignedRoomNumber.indexOf(FloorNumber) > -1)
return false;
return true;
}
为floor_number
添加另一个字段:
<input id="floor_number" name="floor_number" type="number" class="form-control validate-required" min="0" value="" />
如果room_number
和floor_number
相同并且是拼写错误,请使用以下代码:
function validateRoomNumber() {
var FloorNumber = $("#room_number").val().trim();
if (arrayOfAssignedRoomNumber.indexOf(FloorNumber) > -1)
return false;
return true;
}
答案 1 :(得分:0)
检查工作代码段 有一个拼写错误&#34; removeVlass&#34;
并且有声明$(&#34;#floor_number&#34;)。没有输入&#34; floor_number&#34; id目前。如此chnaged
var FloorNumber = $("#room_number").val().trim();
$(":input.validate-required").on('keyup', function() {
$(this).closest(".form-group").removeClass("has-error");
$(this).next('small').addClass('hide');
});
$("#room_number").on('keyup', function() {
if (validateRoomNumber() == false) {
$(this).closest(".form-group").addClass("has-error");
$("#duplicate_error").removeClass('hide');
}
});
function validateRoomNumber() {
var FloorNumber = $("#room_number").val().trim();
var i;
var arrayOfAssignedRoomNumber = ["10","20", "30", "40", "50"];
for (i = 0; i < arrayOfAssignedRoomNumber.length; i++) {
var AssignedFloorNumber = arrayOfAssignedRoomNumber[i];
if (AssignedFloorNumber == FloorNumber) {
return false;
}
}
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="form-group required">
<label class="col-sm-3 control-label">Room Number</label>
<div class="col-sm-4">
<input id="room_number" name="room_number" type="number" class="form-control validate-required" min="0" value="" />
<small id="room_number_help" class="text-danger hide">Room Number is Required</small>
<small id="duplicate_error" class="text-danger hide">Room Number is Already Assigned</small>
</div>
</div>
&#13;
答案 2 :(得分:0)
我已经改变了你的代码
function validateRoomNumber(roomnum)
{
for (i = 0; i < arrayOfAssignedRoomNumber.length; i++) {
var AssignedFloorNumber = arrayOfAssignedRoomNumber[i];
if (AssignedFloorNumber == roomnum) {
return false;
}
}
}
jQuery(document).ready(function ($) {
$(":input.validate-required").on('keyup', function () {
$(this).closest(".form-group").removeClass("has-error");
$(this).next('small').addClass('hide');
});
$("#room_number").on('keyup', function () {
if (validateRoomNumber($(this).val()) == false)
{
$(this).closest(".form-group").addClass("has-error");
$("#duplicate_error").removeClass('hide');
} else {
$("#duplicate_error").addClass('hide');
}
});
});