admin管理员组文章数量:1357394
I am using an e-merce platform that renders the shopping cart as below inside a single div, and I would like to remove everything after the number of items in the cart, starting with the "," through to the end of "Cart".
The number of items and the Total could be any value, so I would need something that wouldn't rely upon a set variable. This would also have to work dynamically as items could be added to the cart after the page loads and would need to update accordingly.
As I am rather new to Javascript and JQuery, I would very much appreciate any help that includes the full script.
1 item(s), Total: $25.19 View Cart
I am using an e-merce platform that renders the shopping cart as below inside a single div, and I would like to remove everything after the number of items in the cart, starting with the "," through to the end of "Cart".
The number of items and the Total could be any value, so I would need something that wouldn't rely upon a set variable. This would also have to work dynamically as items could be added to the cart after the page loads and would need to update accordingly.
As I am rather new to Javascript and JQuery, I would very much appreciate any help that includes the full script.
1 item(s), Total: $25.19 View Cart
Share Improve this question asked Nov 26, 2012 at 21:07 EricEric 211 silver badge2 bronze badges 4-
1
var con = $("#cartelementid"); con.text(con.text().split(',')[0]);
– Shmiddty Commented Nov 26, 2012 at 21:10 - 1 there should be a return value containing number of items, you shouldnt have to make the split. Investigate the api for what you're using. – SpYk3HH Commented Nov 26, 2012 at 21:11
- To avoid splitting you could always do THIS – adeneo Commented Nov 26, 2012 at 21:18
- Possible duplicate, stackoverflow./q/5631384/425313 – Brad Koch Commented Apr 24, 2013 at 15:46
2 Answers
Reset to default 6Try
var text = "1 item(s), Total: $25.19 View Cart";
text = text.split(",")[0];
With a regex replace - /,.*/
will find a ma followed by any number of other characters:
var text = "1 item(s), Total: $25.19 View Cart";
text = text.replace(/,.*/, "");
"This would also have to work dynamically as items could be added to the cart after the page loads and would need to update accordingly."
Well then you'd need to call the above code from wherever new items are added.
本文标签: Remove text after a certain character with Javascript or JQueryStack Overflow
版权声明:本文标题:Remove text after a certain character with Javascript or JQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743954700a2567921.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论