最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery Removing last two characters in a class - Stack Overflow

programmeradmin0浏览0评论

This should be pretty simple. I'm trying to use the slice method to remove the last two characters in a dynamically created string in a shopping cart.

So instead of having a product show as $28.00, I want the product to show up as $28. Since these values are coming from a database, I can't simply define the string in a variable, like I've seen in a lot of tutorials.

I've created a JSFiddle here: /

The jQuery that's not working is below:

    $(".myclass").slice(0,-2);

This should be pretty simple. I'm trying to use the slice method to remove the last two characters in a dynamically created string in a shopping cart.

So instead of having a product show as $28.00, I want the product to show up as $28. Since these values are coming from a database, I can't simply define the string in a variable, like I've seen in a lot of tutorials.

I've created a JSFiddle here: http://jsfiddle.net/EbckS/

The jQuery that's not working is below:

    $(".myclass").slice(0,-2);
Share Improve this question asked Mar 27, 2013 at 23:24 JaggsWaggertJaggsWaggert 1711 gold badge1 silver badge12 bronze badges 1
  • 1 Don't you have to remove the last three to go from "$28.00" to "$28"? – Mike Samuel Commented Mar 27, 2013 at 23:29
Add a comment  | 

2 Answers 2

Reset to default 18

You should use text.

$(".slice").text(function(i, text) {
    return text.slice(0, -2);
});
  • i Reffers the index position of the element in the set
  • text Reffers the old text value

Refference

Very Simple. I uses this for getting number from a string you can use it too.

var height= "800px";

var newheight = height.substring(0,height.length-2); //string 800
newheight= parseFloat(newheight) || 0; //number 800
发布评论

评论列表(0)

  1. 暂无评论