如何使此Javascript函数在没有枚举器的情况下工作?

时间:2018-11-27 10:57:11

标签: javascript

我的问题

我在下面的Javascript函数中尝试修改代码,以便该功能可与Chome和其他浏览器一起使用。 (PS:我发现Enumerator构造函数不是ECMA标准的一部分,它是对该语言的Microsoft JScript附加功能。为了使我的代码适用于最新的浏览器,我需要使用for..in循环来迭代对象)) / p>

我的状态

我是Javascript的初学者,我发现很难找出如何使用for..in循环来迭代对象的方法。我还有其他7种类似的功能,但是我敢肯定,如果有人可以帮助我完成这一任务,我会自己解决其他问题。另外,for..in只是建议的一件事,如果还有其他有效的方法可以解决这种情况,那么它们也是最受欢迎的。

我的JAVASCRIPT功能

除了代码中存在的注释外,我还使用/ * * /放置了额外的注释,以便您可以看到使用了objEnum的要点,并据此建议可以做什么。下面的代码在3个地方。

function validateRequiredFieldsAllFieldsEnabledAndVisible(objFrm) {
    var objElement;
    var objEnum;
    var blnError = false;
    var blnIsChecked;
    var objRadios;


    //Gets Form contents collection
    objEnum = new Enumerator(objFrm); /* 1st Occurence */

    for(;!objEnum.atEnd();objEnum.moveNext()) /* 2nd Occurence */{
        //Gets Next Element in collections
        objElement = objEnum.item(); /* 3rd Occurence */

        //Evaluates if the element is required
        if(objElement.className == "Required") {
            //Evaluates the Element's type
            switch (objElement.tagName) {
                case "SELECT":
                    if(objElement.value == "0" || objElement.value == "") //See if element has not been filled out
                       blnError = true;

                    break;
                case "INPUT":
                    switch(objElement.type) {
                        case "radio":
                            blnIsChecked = false;

                            objRadios = document.getElementsByName(objElement.Name); //Gets the Collection of radio buttons

                            if(objRadios.length > 0)
                            {
                               //traverse the radio collection to see if there is one checked
                                for(var intCount=0;intCount<=objRadios.length - 1;intCount++) {
                                    if(objRadios[intCount].checked) {
                                        blnIsChecked = true;
                                        break;
                                    }
                                }

                                if(!blnIsChecked)
                                    blnError = true;
                            }

                            break;
                        default:
                            if(trim(objElement.value) == "") //See if element has not been filled out
                               blnError = true;
                    }

                    break;
            }

            if(blnError) {
                    alert("Please Fill out the required fields.");
                    return false;
            }
        }
    }

    return true;
}

0 个答案:

没有答案