当我在任何地方放置javascript警报时代码有效

时间:2010-06-30 06:42:20

标签: javascript jquery

我有一个在关键选项卡上运行的功能,当我在代码,任何类型的警报之间放置javascipt警报时,它可以正常工作,如果我删除它停止工作的警报:我的功能

//Function to set the tab feture for focus to work properly on fields with autosuggestion(location and name)
function setFocusOnTab(name) {


    var focusElement = "";

    if(name == "name") {//For main contact field

        if ($("#email").is(":visible")) {
            $('#email').focus();
        }
    } else if(name == 'location_name') {//For main contact field
        $("#country").focus();
    } else {//For extra contact field


        var outputDataCurrentVal = name.lastIndexOf('record_');


        if(outputDataCurrentVal < 0) {

            //ADDTIONAL CONTACT TAB
            var outputDataCurrentName = name.lastIndexOf('_name_');
            if(outputDataCurrentName >= 0) {
                //Replacing the  name to get location name.
                locName = currentName.replace("_name_","_designation_"); 
                focusElement = locName;
            } else {

                var outputDataCurrentLoc = name.lastIndexOf('_location_');  
                if(outputDataCurrentLoc >= 0) {
                    //Replacing the location name to get country name.
                    countryName = name.replace("_location_","_country_"); 
                    focusElement = countryName;
                }
            }

        } else {


            //Extra CONTACT TAB
            var outputDataCurrentName = name.lastIndexOf('_name_');
            if(outputDataCurrentName >= 0) {
                //Replacing the  name to get location name.
                locName = currentName.replace("_name_","_location_"); 
                focusElement = locName;
            } else {

                var outputDataCurrentLoc = name.lastIndexOf('_location_');  
                if(outputDataCurrentLoc >= 0) {
                    //Replacing the location name to get country name.
                    countryName = name.replace("_location_","_country_"); 
                    focusElement = countryName;

                }
            }
        }



        $("#" + focusElement).focus();
        return false;
    }


}

3 个答案:

答案 0 :(得分:3)

听起来你需要一些东西来暂停你的代码,这就是alert()所做的。

您可能需要回调。

答案 1 :(得分:1)

可能alert()发生的事情是,调用它会导致当前窗口失去焦点(当焦点移动到新的弹出对话框时),并且在它完成之后重新聚焦窗口。这将触发焦点和模糊事件,这可能会使您的脚本感到困惑,而在Safari中窗口可能根本不会重新聚焦。

我不清楚你在这做什么...你如何将这个代码附加到tab键事件? return false;应该取消什么事件?如果您使用的是keypress,那么就不会在IE,Safari或Chrome中调用Tab键。如果您使用keydown,则取消该事件不会阻止Opera中的Tab键。那么Shift-Tab呢?

重现/改变浏览器键盘的行为很难:如果有其他任何方式,最好不要这样做。为了使控件像下拉建议器工作,你可能更好的方法是在有关元素上设置声明性tabIndex,并让浏览器解决如何从那里整理标签。

答案 2 :(得分:0)

我应该在这篇文章上写点什么。我阅读了很多博客和帖子,但无法从其他任何地方获得正确的解决方案。即使在这篇文章中,我也会查看更多细节并尝试各种解决方案。

最后阅读了bobince的答案,我可以找到解决方案。在我的情况下,我已将焦点设置为另一个文本框(不是必需类型),稍后当我完成工作时,我将焦点设置为原始文本框。所以故事情节我们只需要将焦点设置在当前元素的其他地方,实际上是通过警报完成的,我已经将焦点放在非必需元素上来替换它。