【问题标题】:How to get individual values of a variable while using foreach in jquery在jquery中使用foreach时如何获取变量的单个值
【发布时间】:2014-06-09 09:45:37
【问题描述】:

我在 jquery 中使用了一个函数,它从服务器端获取所有值,然后根据这些值,在客户端执行一些验证。下面是我用过的代码

AOT.View.AssetMarkTrustTrust.prototype.validateSubmit = function () {    
var result = false;
var message = '';
result = applicationView.GetCardEmbossings();//this method will get the values from the server side

if (result != null)
    $.each(result, function () {
        if ((result.CardEmbossingLine1.Length > 26) ||
                   (!string.IsNullOrEmpty(result.CardEmbossingLine2) && result.CardEmbossingLine2.Length > 26) && !result.Suppress) {
            AOT.Utility.displayWarning("Please use Card/Check Editor to specify name that will be printed on card, 26 characters max.");
            return false;
        }
    });

if (!result && !AOT.Utility.isNullEmptyUndefined(message)) {
    AOT.Utility.displayWarning(message, '');
}
return result;

};

问题是当我检查值 CardEmbossingLine1 的长度时,它显示为未定义。但是,对象结果下有值。我无法满足条件,因为对象下的不同值未定义,因此条件没有被触发。请让我知道我错过了哪里。

谢谢

【问题讨论】:

标签: jquery asp.net-mvc foreach


【解决方案1】:

“each”中的函数可以接受 2 个参数,这些参数是在 jquery 遍历结果对象时设置的。参见下面的代码

if (result != null)
$.each(result, function (index,eachResult) {
    if ((eachResult.CardEmbossingLine1.Length > 26) ||
               (!string.IsNullOrEmpty(eachResult.CardEmbossingLine2) && eachResult.CardEmbossingLine2.Length > 26) && !eachResult.Suppress) {
        AOT.Utility.displayWarning("Please use Card/Check Editor to specify name that will be printed on card, 26 characters max.");
        return false;
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多