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';
}
I have a jquery script to append 'M' to the time string for eg: 'xxx A' to 'xxx AM'. The script works in IE8, IE9, Firefox but does not work in compatibility mode and IE 7. $(this)[0].value[0] is undefined in IE7 and browser in compatibility mode. Please provide alternate solution.
Thank in advance.
You cannot get individual characters from a string using [n]
in IE.
Instead, you should call charAt
:
this.value.charAt(this.value.length - 1)
I think you're looking for something like this:
$(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
);
});
精彩评论