2011-04-15 53 views
0
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中不起作用。在IE7和瀏覽器兼容模式下,$(this)[0] .value [0]未定義。請提供備用解決方案。jQuery長度方法和IE7

提前致謝。

+3

從未有寫原因'$(本)[0]'。 – SLaks 2011-04-15 21:38:06

+0

「this」是指什麼......需要一些上下文來幫助你在這裏... – Ryan 2011-04-15 21:38:14

+0

此外,'length'不是一種方法,並且與jQuery無關。 – SLaks 2011-04-15 21:39:39

回答

0

在IE中,您無法使用[n]從字符串中獲取單個字符。 相反,你應該叫charAt

this.value.charAt(this.value.length - 1) 
0

我認爲你在尋找這樣的事情:

$(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 
       ); 
});