javascript - jQUery 代码在 IE 中不起作用

标签 javascript jquery ajax internet-explorer

这是我的 jQuery 代码。如果用户未填写所有字段,它会显示一些提示消息。

$('#register').click(function(){
    var sign_up_button = $(this).val();

    var pass = $('#password').val();
    var retype_pass = $('#retype_password').val();

    if (pass != retype_pass)
    {
        return false;
    } else if ($('#firstname').val() == '' || $('#lastname').val() == '' || $('#mobile_number').val() == '' || $('#email_address').val() == '' || $('#password').val() == '' || $('#retype_password').val() == '') {
        $('#danger_container_all_fields').attr('class', 'alert alert-danger');
        $('#message_fill_up_all_fields').html("Please, Fill up all fields!");
        return false;
    } else {
        return true;
    }
});

我还在网上看到了 IE 中占位符的 JS 代码,但其中存在一些冲突,抑制了我的 jQuery 代码的消息。

var _debug = false;
var _placeholderSupport = function() {
    var t = document.createElement("input");
    t.type = "text";
    return (typeof t.placeholder !== "undefined");
}();

window.onload = function() {
    var arrInputs = document.getElementsByTagName("input");
    var arrTextareas = document.getElementsByTagName("textarea");
    var combinedArray = [];
    for (var i = 0; i < arrInputs.length; i++)
        combinedArray.push(arrInputs[i]);
    for (var i = 0; i < arrTextareas.length; i++)
        combinedArray.push(arrTextareas[i]);
    for (var i = 0; i < combinedArray.length; i++) {
        var curInput = combinedArray[i];
        if (!curInput.type || curInput.type == "" || curInput.type == "text" || curInput.type == "textarea")
            HandlePlaceholder(curInput);
        else if (curInput.type == "password")
            ReplaceWithText(curInput);
    }

    if (!_placeholderSupport) {
        for (var i = 0; i < document.forms.length; i++) {
            var oForm = document.forms[i];
            if (oForm.attachEvent) {
                oForm.attachEvent("onsubmit", function() {
                    PlaceholderFormSubmit(oForm);
                });
            }
            else if (oForm.addEventListener)
                oForm.addEventListener("submit", function() {
                    PlaceholderFormSubmit(oForm);
                }, false);
        }
    }
};

function PlaceholderFormSubmit(oForm) {    
    for (var i = 0; i < oForm.elements.length; i++) {
        var curElement = oForm.elements[i];
        HandlePlaceholderItemSubmit(curElement);
    }
}

function HandlePlaceholderItemSubmit(element) {
    if (element.name) {
        var curPlaceholder = element.getAttribute("placeholder");
        if (curPlaceholder && curPlaceholder.length > 0 && element.value === curPlaceholder) {
            element.value = "";
            window.setTimeout(function() {
                element.value = curPlaceholder;
            }, 100);
        }
    }
}

function ReplaceWithText(oPasswordTextbox) {
    if (_placeholderSupport)
        return;
    var oTextbox = document.createElement("input");
    oTextbox.type = "text";
    oTextbox.id = oPasswordTextbox.id;
    oTextbox.name = oPasswordTextbox.name;
    //oTextbox.style = oPasswordTextbox.style;
    oTextbox.className = oPasswordTextbox.className;
    for (var i = 0; i < oPasswordTextbox.attributes.length; i++) {
        var curName = oPasswordTextbox.attributes.item(i).nodeName;
        var curValue = oPasswordTextbox.attributes.item(i).nodeValue;
        if (curName !== "type" && curName !== "name") {
            oTextbox.setAttribute(curName, curValue);
        }
    }
    oTextbox.originalTextbox = oPasswordTextbox;
    oPasswordTextbox.parentNode.replaceChild(oTextbox, oPasswordTextbox);
    HandlePlaceholder(oTextbox);
    if (!_placeholderSupport) {
        oPasswordTextbox.onblur = function() {
            if (this.dummyTextbox && this.value.length === 0) {
                this.parentNode.replaceChild(this.dummyTextbox, this);
            }
        };
    }
}

function HandlePlaceholder(oTextbox) {
    if (!_placeholderSupport) {
        var curPlaceholder = oTextbox.getAttribute("placeholder");
        if (curPlaceholder && curPlaceholder.length > 0) {
            Debug("Placeholder found for input box '" + oTextbox.name + "': " + curPlaceholder);
            oTextbox.value = curPlaceholder;
            oTextbox.setAttribute("old_color", oTextbox.style.color);
            oTextbox.style.color = "#c0c0c0";
            oTextbox.onfocus = function() {
                var _this = this;
                if (this.originalTextbox) {
                    _this = this.originalTextbox;
                    _this.dummyTextbox = this;
                    this.parentNode.replaceChild(this.originalTextbox, this);
                    _this.focus();
                }
                Debug("input box '" + _this.name + "' focus");
                _this.style.color = _this.getAttribute("old_color");
                if (_this.value === curPlaceholder)
                    _this.value = "";
            };
            oTextbox.onblur = function() {
                var _this = this;
                Debug("input box '" + _this.name + "' blur");
                if (_this.value === "") {
                    _this.style.color = "#c0c0c0";
                    _this.value = curPlaceholder;
                }
            };
        }
        else {
            Debug("input box '" + oTextbox.name + "' does not have placeholder attribute");
        }
    }
    else {
        Debug("browser has native support for placeholder");
    }
}

function Debug(msg) {
    if (typeof _debug !== "undefined" && _debug) {
        var oConsole = document.getElementById("Console");
        if (!oConsole) {
            oConsole = document.createElement("div");
            oConsole.id = "Console";
            document.body.appendChild(oConsole);
        }
        oConsole.innerHTML += msg + "<br />";
    }
}

有人可以帮我找到冲突代码吗?顺便说一句,这段代码仅在 IE 中发生冲突。在其他浏览器中它运行得非常好。

最佳答案

问题在于这段代码:

if (oForm.attachEvent) {
    oForm.attachEvent("onsubmit", function() {
        PlaceholderFormSubmit(oForm);
    });
}

正在替换 jQuery 提交处理程序。它只发生在 IE 中,因为 attachEvent 是一个非标准的、仅限 IE 的方法。您有多种选择来解决此问题:

  1. 只需去掉上面的代码即可。那么这部分占位符代码在老版本的IE中就无法工作了(最近版本有addEventListener)。

  2. 更改占位符代码中 ifelse if block 的顺序,以便首先测试 addEventListener。这样,覆盖只会发生在旧版本的 IE 中。一般来说,最好先测试新功能,只有在旧方法不存在时才使用旧方法。

  3. 在占位符代码之后运行 jQuery 代码。这样,它将把它的提交处理程序添加到占位符添加的提交处理程序中。

关于javascript - jQUery 代码在 IE 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29292131/

相关文章:

javascript - 以文件形式提交 HTML 表单数据/合并表单数据 + 文件并在 Javascript 中作为单个文件发送

javascript - 如何从输入标签中正确获取值?

javascript - 使答案不区分大小写

javascript - 从 onClick 事件捕获链接

javascript - 如何在 Ajax 期间迭代嵌套 JSON?

Javascript 用缺少的对象和值填充数组

jquery - .toggle() 示例不适用于 jQuery 1.9

javascript - jquery计算BMI

javascript - 在php中使用jquery ajax删除从另一个页面获取的结果

jquery - Angularjs 中 jQuery ajax beforeSend 的等价物是什么?