开发者

Make initial capital in a user-supplied control name

I have a function with the name MakeInitialCapital. I want to supply control name as an argument and the text in that control 开发者_如何转开发must be converted to Initial Capital.

How to modify this Javascript code:

function MakeInitialCapitalControl(controlName)
{
    TextInControl = getElementByID(controlName).value;  
    return str.toLowerCase().replace(/\b[a-z]/g, cnvrt);
function cnvrt() {
    return arguments[0].toUpperCase();
}

Edited:

One more thing, if the text already has Intials Capital, make it lowercase.


Assuming the rest of your code works, you need to assign the value, rather than returning it:

function MakeInitialCapitalControl(controlName)
{
    var ctrl = getElementByID(controlName);
    ctrl.value = ctrl.value.toLowerCase().replace(/\b[a-z]/g, function {
        return arguments[0].toUpperCase();
    });
}

EDIT

As for the request in your edit... that's a rather strange request. Are you sure you want that in the same function? It'll do pretty much the exact opposite of what the function name implies. But oh well:

function MakeInitialCapitalControl(controlName)
{
    var ctrl = getElementByID(controlName);

    if(/^[A-Z]/.test(ctrl.value)) {
        ctrl.value = ctrl.value.toLowerCase();
        return;
    }        

    ctrl.value = ctrl.value.toLowerCase().replace(/\b[a-z]/g, function {
        return arguments[0].toUpperCase();
    });
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜