jQuery Length方法和IE7

问题描述

|
if (($(this)[0].value[$(this)[0].value.length - 1] == \'A\' || $(this)[0].value[$(this)[0].value.length - 1] == \'P\') && collFormat == 18) {
    $(this)[0].value = $(this)[0].value + \'M\';
}
我有一个jquery脚本将\'M \'附加到时间字符串中,例如:\'xxx A \'到\'xxx AM \'。该脚本可在IE8,IE9,Firefox中运行,但不能在兼容模式和IE 7中运行。$(this)[0] .value [0]在IE7和兼容模式下的浏览器中未定义。请提供替代解决方案。 预先感谢。     

解决方法

您无法在IE中使用
[n]
从字符串中获取单个字符。 相反,您应该致电
charAt
this.value.charAt(this.value.length - 1)
    ,我认为您正在寻找这样的东西:
$(this).val(function(i,oldVal) { // set the element\'s value to the return value of this function
    var lastChar = oldVal.substr(-1); // get the last character of the current value

    return oldVal + // have the original value with something added
               (
                   (lastChar == \'A\' || lastChar == \'P\') && collFormat == 18) ? // if this is the case
                   \'M\' : // add an M
                   \'\' // otherwise,add an empty string
               );
});
    

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...