admin管理员组

文章数量:1313170

How can I change each labels first character except the last labels character $ sign to £ using jQuery?

Here is the code:

<p align="center" id="myradio">
    <label>$25.00 </label>
    <input checked="checked" class="hide_input" name="amount" type="radio" value="25.00" />
    <label>$50.00</label>
    <input class="hide_input" name="amount" type="radio" value="50.00" />
    <label>$100.00</label>
    <input class="hide_input" name="amount" type="radio" value="100.00" />
    <label>$250.00</label>
    <input class="hide_input" name="amount" type="radio" value="250.00" />
    <label>$500.00</label>
    <input class="hide_input" name="amount" type="radio" value="500.00" />
    <label>$1000.00</label>
    <input class="hide_input" name="amount" type="radio" value="1000.00" />
    <label>other amount</label>
    <input class="show_input" id="other" name="amount" type="radio" value="" />
</p>

How can I change each labels first character except the last labels character $ sign to £ using jQuery?

Here is the code:

<p align="center" id="myradio">
    <label>$25.00 </label>
    <input checked="checked" class="hide_input" name="amount" type="radio" value="25.00" />
    <label>$50.00</label>
    <input class="hide_input" name="amount" type="radio" value="50.00" />
    <label>$100.00</label>
    <input class="hide_input" name="amount" type="radio" value="100.00" />
    <label>$250.00</label>
    <input class="hide_input" name="amount" type="radio" value="250.00" />
    <label>$500.00</label>
    <input class="hide_input" name="amount" type="radio" value="500.00" />
    <label>$1000.00</label>
    <input class="hide_input" name="amount" type="radio" value="1000.00" />
    <label>other amount</label>
    <input class="show_input" id="other" name="amount" type="radio" value="" />
</p>
Share Improve this question edited Apr 26, 2012 at 16:36 noob 9,2124 gold badges39 silver badges65 bronze badges asked Apr 26, 2012 at 16:32 user1235905user1235905 1457 bronze badges 1
  • Your last label doesn't have any instance of the $ sign. – Sampson Commented Apr 26, 2012 at 16:37
Add a ment  | 

4 Answers 4

Reset to default 12

You can use the internal looping to swap the characters:

$("#myradio label").html(function(){
    return this.innerHTML.replace( '$', '£' ); 
});
$("#myradio label:not(:last)").each(function() {
    $(this).text($(this).text().replace(/^\$/, "£"));
});
    $('#myradio label').each(function() {
       $(this).text('£' + $(this).text().substr(1));
    });

This basically takes the substring of the label, beginning from the first character (till the end) - which is the text without the $ symbol. And appends it to the pound symbol. This concatenated text is used tot replace the existing text.

$('#myradio label').html(function() {
   return this.innerHTML.replace(/^\$/,'£');
});

本文标签: javascriptReplace first character inside the labelStack Overflow