开发者

Javascript 'undefined error' in asp.net webform

I'm not a javascript expert, but I've used it before, and this problem is perplexing me. Here's the javascript:

function formatNumThousands(myNumber) {
    if (myNumber.length > 0) {
        var index = myNumber.indexOf(',');
        while (index != -1) {
             myNumber = myNumber.replace(',', '');
             index = myNumber.indexOf(',');
        }
        var myResult = '';
        for (var i = myNumber.length - 1; i >= 0; i--) {
            myResult = myNumber[开发者_Go百科i] + myResult;
            if ((myNumber.length - i) % 3 == 0 & i > 0) myResult = ',' + myResult;
        }
        document.getElementById('<%=txtMyNumber.ClientID%>').value = myResult;
     }
  }

In the code behind, I set the attribute for this text box's OnBlur event:

this.txtMyNumber.Attributes.Add("onblur", "formatNumThousands(this.value);");

I build and publish the app and it runs fine on my machine. Two other users get 'undefined' in the text box, regardless of what they do.


Try this trimmed version (see in jsfiddle). See that I replaced the [] operator for the charAt() method, as it's a more conservative approach for extracting individual characters from a string:

function formatNumThousands(editElement) {
    if (!editElement || editElement.value.length == 0)
        return;

    var myNumber = editElement.value.replace(/,/g, '');

    var myResult = '';
    for (var i = myNumber.length - 1; i >= 0; i--) {
        myResult = myNumber.charAt(i) + myResult;
        if ((myNumber.length - i) % 3 == 0 & i > 0)
            myResult = ',' + myResult;
    }

    editElement.value = myResult;
}

Don't forget to change the code behind to

// Notice that `.value´ is gone:
this.txtMyNumber.Attributes.Add("onblur", "formatNumThousands(this)");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜