简体   繁体   English

Javascript无法用于禁用的文本框

[英]Javascript doesn't work for disabled text box

This is my Javascript: 这是我的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. 还要注意,我使用了类选择器.disabled-textbox ,您可能需要将此类添加到文本框中,或者使用更智能的选择器来获取所有禁用的文本框。 Here's the jsfiddle that shows how to update textbox. 这是显示如何更新文本框的jsfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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