简体   繁体   中英

Javascript doesn't work for disabled text box

This is my Javascript:

<script type = "text/javascript" language = "javascript">
  function formatCurrency(num) {
    num = num.toString().replace(/\Rs.|\,/g, '');
    if (isNaN(num))
      num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
      cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
      num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + num + '.' + cents);
  }
</script>

And I am using this script to format the currency in a text box. It is working for all enabled text boxes, but it doesn't work for disabled text boxes, as the same amount comes in text box on some event.

Below is my text box:

<asp:TextBox ID="txtToTSanctioned" runat="server" Text="00.00"
  CssClass="mytextbox" Enabled="False"
  onblur = "this.value=formatCurrency(this.value);"></asp:TextBox>

What'd be the solution for that?

One of the ways to do it, is to execute it for a specific textbox. As in your case it's not enabled and value is set programmatically, you can update it value after it's populated:

var oldValue = $('.disabled-textbox').val();
var formattedValue = formatCurrency( oldValue );
$('.disabled-textbox').val( formattedValue );

Now you only need to execute this code when your textbox was populated. Also note, I used class selector .disabled-textbox , you may need to add this class to your textbox, or use a smarter selector to get all disabled textboxes. Here's the jsfiddle that shows how to update textbox.

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