简体   繁体   中英

jQuery How to Target A String in a TD Class

I'm trying to subtract a two characters from a string that is being dynamically generated from a database that is being placed into a shopping cart table td . 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.

Here's my JS Fiddle http://jsfiddle.net/EbckS/6/

Here's my eronous code

    $(document).ready(function(){

       $("table td.SCNProductPrice").text(function(i, text) {
       return text.slice(0, -2);
       });

    });

This is a follow up to a different question I posted here: jQuery Removing last two characters in a class

I'm placing this into a different question because I didn't realize targeting a class within a table td would require different syntax. Thanks for your help!

Assuming your html is valid ie it has table and td your slice has wrong index. First one will take care of any number of decimal points if at all comes up.

try

$("table td.SCNProductPrice").text(function(i, text) {
    return '$' + parseInt(text.replace('$',''));
});

or

 $("table td.SCNProductPrice").text(function(i, text) {
       return text.slice(0, -3);
       });

You should add td inside a tr which is inside a table .

<table>
    <tr>
        <td class="SCNProductPrice" valign="top">$28.00</td>
    </tr>
</table>

Demo

Markup:

<table>
    <tr>
        <td class="SCNProductPrice" valign="top">$28.00</td>
    </tr>
</table>

JS:

$(document).ready(function(){
    $("td.SCNProductPrice").text(function(i,text) {
         return text.split('.')[0];
    });
});

don't forget, you need valid markup during your fiddle. lastly, i'm not sure why you'd want "$28." shouldn't it just be "$28" ? The JS above does the latter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM