[英]How can I compare the key to a string in a JQuery $.each loop?
I'm trying to get the error message out of a custom data attribute JQuery unobtrusive adapter, but I can't seem to extract the error message into a variable. 我正在尝试从自定义数据属性JQuery非侵入式适配器中获取错误消息,但似乎无法将错误消息提取到变量中。 My validation message just returns as:
我的验证消息仅返回为:
Warning: No message defined for Image
警告:没有为图片定义消息
The code is as follows: 代码如下:
$(document).ready(function () {
var errorMessage;
$.validator.unobtrusive.adapters.add(
'filesize', ['maxsize'], function (options) {
options.rules['filesize'] = options.params;
if (options.message) {
options.message['filesize'] = options.message;
$.each(options, function (key, val) {
console.log("Key: " + key + " | Value: " + val);
if (key === "message") {
errorMessage = val;
}
});
}
});
$.validator.addMethod('filesize', function (value, element, params) {
if (element.files.length < 1) {
// No files selected
return true;
}
if (!element.files || !element.files[0].size) {
// This browser doesn't support the HTML5 API
return true;
}
return element.files[0].size < params.maxsize;
}, errorMessage); // This is where the variable errorMessage is used
});
Also, when I use the correct JQuery syntax of options.messages (plural) and leave out the whole $.each block, my Firefox crashes when I open Firebug? 另外,当我使用options.messages(复数)的正确JQuery语法并忽略整个$ .each块时,当我打开Firebug时,Firefox崩溃了吗?
You'd normally specify the error message at the custom validation attribute that you wrote on your view model: 通常,您需要在视图模型上编写的自定义验证属性中指定错误消息:
public class MyViewModel
{
[Required]
[MaxFileSize(8388608, ErrorMessage = "Maximum allowed file size is {0} bytes")]
public HttpPostedFileBase File { get; set; }
}
and then have a custom validation attribute: 然后具有自定义验证属性:
public class MaxFileSizeAttribute : ValidationAttribute, IClientValidatable
{
private readonly int _maxFileSize;
public MaxFileSizeAttribute(int maxFileSize)
{
_maxFileSize = maxFileSize;
}
public override bool IsValid(object value)
{
var file = value as HttpPostedFileBase;
if (file == null)
{
return false;
}
return file.ContentLength <= _maxFileSize;
}
public override string FormatErrorMessage(string name)
{
return base.FormatErrorMessage(_maxFileSize.ToString());
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule
{
ErrorMessage = FormatErrorMessage(_maxFileSize.ToString()),
ValidationType = "filesize"
};
rule.ValidationParameters["maxsize"] = _maxFileSize;
yield return rule;
}
}
and the unobtrusive adapter: 和不打扰的适配器:
jQuery.validator.unobtrusive.adapters.add(
'filesize', [ 'maxsize' ], function (options) {
options.rules['filesize'] = options.params;
if (options.message) {
options.messages['filesize'] = options.message;
}
}
);
jQuery.validator.addMethod('filesize', function (value, element, params) {
if (element.files.length < 1) {
// No files selected
return true;
}
if (!element.files || !element.files[0].size) {
// This browser doesn't support the HTML5 API
return true;
}
return element.files[0].size < params.maxsize;
}, '');
To compare a key to a string with $.each you can simply do 要将键与$。字符串进行比较,您只需执行以下操作
var myObj = {'message1' : 1, 'message2' : 2};
$.each(myObj, function(key){
if(key == 'message1'){
alert('they match!');
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.