【发布时间】:2016-03-20 22:07:35
【问题描述】:
我正在处理一个包含多个字段的表单,当“onblur”发生时,jquery validate 正在将错误类/标签添加到有效字段中。
这是我的表单 html
<div id="diaAddCustomer" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true" data-backdrop="static">
<div class="modal-dialog" style="background-color: #fff; width: 650px;">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true"> </span></button>
<h4 class="modal-title">Add New Customer</h4>
</div>
<form class="form-horizontal" id="frmAddCustomer" name="frmAddCustomer" novalidate>
<input type="hidden" id="addCustomerPersonID" name="addCustomerPersonID" value="">
<div class="modal-body">
<div class="form-group">
<div class="col-md-3">
<label for="addCustomerName">Name:</label>
</div>
<div class="col-md-9">
<input type="text" class="form-control input-sm" id="addCustomerName" name="addCustomerName" value="">
</div>
</div>
<h4>Billing Address</h4>
<div class="form-group">
<div class="col-md-3">
<label for="addCustomerAdd1">Address:</label>
</div>
<div class="col-md-9">
<input type="text" class="form-control input-sm" id="addCustomerAdd1" name="addCustomerAdd1" value="">
</div>
</div>
<div class="form-group">
<div class="col-md-3">
<label for="addCustomerAdd2"></label>
</div>
<div class="col-md-9">
<input type="text" class="form-control input-sm" id="addCustomerAdd2" name="addCustomerAdd2" value="">
</div>
</div>
<div class="form-group">
<div class="col-md-3">
<label for="addCustomerCity">City:</label>
</div>
<div class="col-md-9">
<input type="text" class="form-control input-sm" id="addCustomerCity" name="addCustomerCity" value="">
</div>
</div>
<div class="form-group">
<div class="col-md-3">
<label for="addCustomerState">State:</label>
</div>
<div class="col-md-4">
<input type="text" class="form-control input-sm" id="addCustomerState" name="addCustomerState" value="">
</div>
<div class="col-md-2">
<label for="addCustomerZip">Zip:</label>
</div>
<div class="col-md-3">
<input type="text" class="form-control input-sm" id="addCustomerZip" name="addCustomerZip" value="" onkeyup="valNumber($(this))">
</div>
</div>
<div class="form-group">
<div class="col-md-3">
<label for="addCustomerPhone">Phone:</label>
</div>
<div class="col-md-4">
<input type="text" class="form-control input-sm" id="addCustomerPhone" name="addCustomerPhone" onkeyup="valNumber($(this))" onblur="formatPhone($(this))" onfocus="removePhoneFormat($(this))" value="">
</div>
<div class="col-md-2">
<label for="addCustomerPhoneType">Type:</label>
</div>
<div class="col-md-3">
<select class="form-control input-sm" id="addCustomerPhonetype" name="addCustomerPhoneType">
<option value=""> Please Select...</option>
<option value="cell"> Cell</option>
<option value="fax"> Fax</option>
<option value="phone"> Phone</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-md-3">
<label for="addCustomerEmail">Email:</label>
</div>
<div class="col-md-9">
<input type="text" class="form-control input-sm" id="addCustomerEmail" name="addCustomerEmail" value="">
</div>
</div>
<h4>Shipping Address <span style="font-size: 10pt;"><input type="checkbox" id="ckbSameShip" ng-click="setShipAddress()"> Same As Billing</span></h4>
<div class="form-group">
<div class="col-md-3">
<label for="addCustomerShipAdd1">Address:</label>
</div>
<div class="col-md-9">
<input type="text" class="form-control input-sm" id="addCustomerShipAdd1" name="addCustomerShipAdd1" value="">
</div>
</div>
<div class="form-group">
<div class="col-md-3">
<label for="addCustomerShipAdd2"></label>
</div>
<div class="col-md-9">
<input type="text" class="form-control input-sm" id="addCustomerShipAdd2" name="addCustomerShipAdd2" value="">
</div>
</div>
<div class="form-group">
<div class="col-md-3">
<label for="addCustomerShipCity">City:</label>
</div>
<div class="col-md-9">
<input type="text" class="form-control input-sm" id="addCustomerShipCity" name="addCustomerShipCity" value="">
</div>
</div>
<div class="form-group">
<div class="col-md-3">
<label for="addCustomerShipState">State:</label>
</div>
<div class="col-md-4">
<input type="text" class="form-control input-sm" id="addCustomerShipState" name="addCustomerShipState" value="">
</div>
<div class="col-md-2">
<label for="addCustomerShipZip">Zip:</label>
</div>
<div class="col-md-3">
<input type="text" class="form-control input-sm" id="addCustomerShipZip" name="addCustomerShipZip" value="" onkeyup="valNumber($(this))">
</div>
</div>
<div class="form-group">
<div class="col-md-3">
<label for="addCustomerShipPhone">Phone:</label>
</div>
<div class="col-md-4">
<input type="text" class="form-control input-sm" id="addCustomerShipPhone" name="addCustomerShipPhone" onkeyup="valNumber($(this))" onblur="formatPhone($(this))" onfocus="removePhoneFormat($(this))" value="">
</div>
<div class="col-md-2">
<label for="addCustomerShipPhoneType">Type:</label>
</div>
<div class="col-md-3">
<select class="form-control input-sm" id="addCustomerShipPhonetype" name="addCustomerShipPhoneType">
<option value=""> Please Select...</option>
<option value="cell"> Cell</option>
<option value="fax"> Fax</option>
<option value="phone"> Phone</option>
</select>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-sm btn-primary"><i class="glyphicon glyphicon-plus"></i> Add Customer</button>
<button type="button" class="btn btn-sm btn-default" data-dismiss="modal"><i class="glyphicon glyphicon-ban-circle"></i> Cancel</button>
</div>
</form>
</div>
这是我的验证脚本
$scope.openAddCustomerWdw = function(pID){
$("#diaAddCustomer").on("shown.bs.modal", function(){
$("#addCustomerPersonID").val(pID);
$("#addCustomerName").val("");
$("#addCustomerAdd1").val("");
$("#addCustomerAdd2").val("");
$("#addCustomerCity").val("");
$("#addCustomerState").val("");
$("#addCustomerZip").val("");
$("#addCustomerPhone").val("");
$("#addCustomerPhoneType").val("");
$("#addCustomerEmail").val("");
$("#addCustomerShipAdd1").val("");
$("#addCustomerShipAdd2").val("");
$("#addCustomerShipCity").val("");
$("#addCustomerShipState").val("");
$("#addCustomerShipZip").val("");
$("#addCusomterShipPhone").val("");
$("#addCustomerShipPhoneType").val("");
$("#addCustomerName").select();
});
$("#diaAddCustomer").modal("show");
$("#frmAddCustomer").validate({
debug: true,
rules: {
addCustomerName: { required: true },
addCustomerAdd1: { required: true },
addCustomerCity: { required: true },
addCustomerState: { required: true },
addCustomerZip: { required: true },
addCustomerPhone: { required: true, phoneUS: true },
addCustomerPhoneType: { required: true },
addCustomerEmail: { required: function(){
return $("#addCustomerEmail").length > 0;
}, email: true },
addCustomerShipAdd1: { required: true },
addCustomerShipCity: { required: true },
addCustomerShipZip: { required: true },
addCustomerShipPhone: { required: true, phoneUS: true },
addCustomerShipPhoneType: { required: true }
},
messages: {
addCustomerName: { required: "Name is Required" },
addCustomerAdd1: { required: "Address is Required" },
addCustomerCity: { required: "City is Required" },
addCustomerState: { required: "State is Required" },
addCustomerZip: { required: "Zip Code is Required" },
addCustomerPhone: { required: "Valid Phone Required", phoneUS: "Valid Phone Required" },
addCustomerPhoneType: { required: "Phone Type Required" },
addCustomerEmail: { required: "Valid Email Required", email: "Valid Email Required" },
addCustomerShipAdd1: { required: "Address is Required" },
addCustomerShipCity: { required: "City is Required" },
addCustomerShipState: { required: "State is Required" },
addCustomerShipZip: { required: "Zip Code is Required" },
addCustomerShipPhone: { required: "Valid Phone Required", phoneUS: "Valid Phone Required" },
addCustomerShipPhoneType: { required: "Phone Type is Required" }
},
success: function(data){
if(data == "success"){
} else {
}
}
})
}
当表单打开时,html 如上所示,没有应用 jquery 标签/错误类。在除第二个地址字段之外的任何字段中输入文本时,会在发生 onblur 时应用 jquery 标签/错误类。它是一个空标签,但会导致表单字段下拉附加标签。
这是表单打开时的样子
这是输入数据后的表格
我意识到 jquery validate 确实对模糊事件进行了表单验证,我理解的那部分工作正常。问题是在有效字段中附加了错误元素,结果导致表单高度扩展,并且没有提供任何信息来说明正在发生的事情。
我尝试过使用 debug: true,查看控制台窗口并尝试以不同方式跟踪此错误,不幸的是,似乎没有任何其他错误信息可供我使用。
非常感谢帮助我指出错误出现在代码中的正确方向。
【问题讨论】:
-
The code you posted does not show the issue。请构建一个更完整的演示。
-
@Sparky 我同意并理解您在说什么,如果我没有在此处发布的代码中发现问题,我会打开我的开发环境让您查看有关正在发生的事情的更多详细信息。问题解决是我的错误,因为没有看到“成功:函数(数据)”,它应该是“提交处理程序:函数(数据)”。按照我的回答中所述进行此修复解决了那些验证字段占用“空间”的问题,而不是像此页面上的所有表单那样没有空间的预期结果。谢谢,您确实促使我仔细查看以发现错误。
标签: jquery forms jquery-validate