如何将焦点设置在出错时弹出窗口的最后一个元素上

How to set focus on the last element for which the window pop ups on error

本文关键字:窗口 最后一个 元素 焦点 设置 出错      更新时间:2023-09-26

根据验证检查字段时,如果验证失败,将打开一个弹出窗口。现在,当我关闭弹出窗口时,弹出窗口应该将焦点返回到最后一个元素(以防多个文本字段的多个验证失败)。有什么建议吗?我的代码是:-

 function submitformFinal(frm) {
 var message;

 var inputs = frm.getElementsByTagName("input");
     for (var i=0; i < inputs.length; i++)
     {
         if (inputs[i].getAttribute('type') == 'text')
         {
             if(!checkSpecialChars(inputs[i].value))
             {
                  message =  " Special characters found in element = '";
                  message += inputs[i].getAttribute('name') + "''n";
                  // alert(message);
                  // createPopup(message);
                  MyPopUpWin(message);
             }
        }
    }    
    // return true;
 }
function checkSpecialChars(fileOnlyName)
{
    if ((/[^a-z0-9'.]/gi).test(fileOnlyName)) {
        return false;
    }
    return true;
}
function MyPopUpWin(message) {
    var iMyWidth;
    var iMyHeight;
    //half the screen width minus half the new window width (plus 5 pixel borders).
    iMyWidth = (window.screen.width/2) - (75 + 10);
    //half the screen height minus half the new window height (plus title and status bars).
    iMyHeight = (window.screen.height/2) - (100 + 50);
    //Open the window.
    var generator = window.open();
    document.onclick=function()   {
        try{
            generator.focus();
            return false
        }
        catch(e){}
    }
    generator.document.write('<html><head><title>Pop uP</title>');
    generator.document.write('<p style="color:#C52B27;">');
    generator.document.write(message);
    generator.document.write('</p>');
    generator.document.write('</head><body>');
    generator.document.write('<a href="javascript:self.close()"><img src="/img/save_orange.gif" border=0">  <'/a>');
   generator.document.write('</body></html>');
   generator.document.close();
}

在代码中,使用:

MyPopUpWin(message); <- existing line
inputs[i].focus()