Javascript焦点不适用于Aspx页面

时间:2011-06-09 07:15:11

标签: javascript

Hy Guys,

请查看代码并尝试帮助。我写的功能不起作用,但正常运行。它的目标是在ASPX页面上使用它来关注页面上的下一个内容。下面是我的代码:

function SetFocusOnSave(CTag,NTag)
{
    alert('Running'+CTag+NTag);

    var CurrentTag=document.getElementById(CTag);    
    var NextTag = document.getElementById(NTag);

    if ( (event.keyCode==13)||(event.keyCode==9) )    
    {    
        if(CurrentTag.value=="")    
        {    
            alert("Please Enter Detail First");    
            CurrentTag.focus();    
        }

        if(CurrentTag.value!="")
        {
            event.returnValue=true;    
            document.getElementById(NextTag).focus();
        }    
    }
}

snametxt.Attributes.Add("onkeypress",    
              SetFocusOnSave('<%=snametxt.ClientID%>','<%=sdesctxt.ClientID%>');");

3 个答案:

答案 0 :(得分:1)

你试过替换

吗?
document.getElementById(NextTag).focus();

NextTag.focus();

答案 1 :(得分:0)

在验证中发现false后,您必须添加return false;,否则流程将持续到结束。

尝试此功能:

function SetFocusOnSave(CTag, NTag) {

    alert('Running' + CTag + NTag);   
    var CurrentTag = document.getElementById(CTag);    
    var NextTag = document.getElementById(NTag);

    if ((event.keyCode == 13) || (event.keyCode == 9))    
    {

        if (CurrentTag.value == "")    
        {

            alert("Please Enter Detail First");    
            CurrentTag.focus();
            return false;
        } 

        if (CurrentTag.value != "") {
            event.returnValue = true;    
            NextTag.focus();
            return false;
        }
    }

};

答案 2 :(得分:0)

Hy Guys Ive尝试了一个新的代码,幸运的是它在我的代码中工作得很好

function Navigation(CTag, NTag, Number) {

    var CurrentTag = document.getElementById(CTag);
    var NextTag = document.getElementById(NTag);
    var IsNumber = Number; //Checking if value is number

    if (NextTag.disabled == true) {
        NextTag.disabled = false;
        NextTag.className = "txt";
    }

    if (event.keyCode == 9) {
        CurrentTag.focus();
        event.returnvalue = false;
    }

    if (event.keyCode != 9) {
        if (event.keyCode == 13) {
            if (IsNumber == "Y") {

                if (NextTag.disabled == true) {
                    NextTag.disabled = false;
                }
                if (CurrentTag.value != "") {
                    NextTag.focus();
                    event.returnvalue = true;
                }
                if (CurrentTag.value == "") {
                    alert('Please Enter Value To Proceed Further.');
                    CurrentTag.focus();
                    event.returnvalue = false;
                }
                if (isNaN(CurrentTag.value)) {
                    alert("Please Enter A Valid Number");
                    CurrentTag.value = "";
                    CurrentTag.focus();
                }
            }
            if (IsNumber == "N") {
                if (CurrentTag.value == "") {
                    alert('Please Enter Value To Proceed Further.');
                    CurrentTag.focus();
                    event.returnvalue = false;
                }
                if (CurrentTag.value != "") {
                    NextTag.focus();
                    event.returnvalue = true;
                }
            }
        }
    }
};

谢谢你! :)