Odpowiedzi:
Możesz także spróbować tego w zwykłym javascript
"1234".slice(0,-1)
ujemny drugi parametr jest przesunięciem od ostatniego znaku, więc możesz użyć -2, aby usunąć 2 ostatnie znaki itp
Po co do tego używać jQuery?
str = "123-4";
alert(str.substring(0,str.length - 1));
Oczywiście, jeśli musisz:
Substr w / jQuery:
//example test element
$(document.createElement('div'))
.addClass('test')
.text('123-4')
.appendTo('body');
//using substring with the jQuery function html
alert($('.test').html().substring(0,$('.test').html().length - 1));
Najlepsze metody użycia to @skajfes i @GolezTrol. Osobiście wolę używać „slice ()”. Jest mniej kodu i nie musisz wiedzieć, jak długi jest łańcuch. Po prostu użyj:
//-----------------------------------------
// @param begin Required. The index where
// to begin the extraction.
// 1st character is at index 0
//
// @param end Optional. Where to end the
// extraction. If omitted,
// slice() selects all
// characters from the begin
// position to the end of
// the string.
var str = '123-4';
alert(str.slice(0, -1));
Możesz to zrobić za pomocą zwykłego JavaScript:
alert('123-4-'.substr(0, 4)); // outputs "123-"
Zwraca to pierwsze cztery znaki ciągu (dostosuj 4do swoich potrzeb).