我在MVC4場景中有以下jQuery驗證程序。它工作正常。但是我只需要在用戶點擊按鈕時進行驗證;而不是當用戶改變文本框的焦點時。我們如何做這個改變?在SelctedValueCheckAttribute如何避免jQuery驗證器觸發文本框標籤
jQuery的
$(function()
{
jQuery.validator.unobtrusive.adapters.add
("lijovalidator", ["param"], function (options)
{
options.rules["lijovalidator"] = options.params.param;
options.messages["lijovalidator"] = options.message;
});
jQuery.validator.addMethod("lijovalidator",
function (value, element, param) {
alert(value);
alert(param);
alert(element);
alert($("#" + param).val());
return false;
});
}(jQuery));
客戶端驗證
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
ModelClientValidationRule mcvr = new ModelClientValidationRule();
mcvr.ValidationType = "enteredvaluescheck";
mcvr.ErrorMessage = "Selected Value must be in given range";
mcvr.ValidationParameters.Add("param", OtherProperty);
mcvr.ValidationType = "lijovalidator";
yield return mcvr;
}
型號
public class CrossFieldValidation
{
public IEnumerable<SelectListItem> Items { get; set; }
[SelctedValueCheckAttribute("TxtCrossField") ]
public string SelectedValue { get; set; }
[Required]
[Display(Name = "Quantity:")]
public string TxtCrossField{ get; set; }
public IEnumerable<SelectListItem> ShippingItems { get; set; }
[SelctedValueCheckAttribute("ShippingValue")]
public string SelectedShippingItemValue { get; set; }
[Display(Name = "Quantity:")]
[Required]
public string ShippingValue { get; set; }
}
也許[這](http://stackoverflow.com/questions/5252373/jquery-validate-rule-only-at-submit)會有所幫助。 – Zabavsky