【问题标题】:jquery validate catching the height of the error containerjquery validate 捕获错误容器的高度
【发布时间】:2010-09-23 17:03:44
【问题描述】:

我在提交错误表单时使用“jquery validate”插件时尝试获取错误容器的高度,但我似乎无法获取错误容器出现时的高度值。警告框甚至没有显示。请问有什么解决办法?

代码如下:

$(".validate").validate({
    rules: {
        j_username: "required"
    },
    submitHandler: function(form) {
        //$(":submit", form).attr("disabled","disabled").val("Please wait....");

        $(":submit", form).attr({
                                disabled: "disabled",
                                value: "Please wait...."
                                });         


        var errH = $("#errorMsgContainer").height();
        if($("#errorMsgContainer").is(":visible")){     
            alert("visible and '#errorMsgContainer' height is: " + errH);
        }
        else{
            alert("Not visible and '#errorMsgContainer' height is: " + errH);
        }

        form.submit();

        return false;
    },
    messages: {
        j_username: "Please type your email address correctly!",
        j_password: "Your password and username do not match!"
    },
    ignore: ".catalogueDD",
    errorLabelContainer: $("#errorMsgContainer")
});

谢谢。

【问题讨论】:

    标签: jquery forms jquery-validate


    【解决方案1】:

    如果元素不占用文档中的任何空间,那么即使它实际上是可见的,is(':hidden') 也会返回 true,这样做可能更安全:

    if( !$('#errorMsgContainer').is(':visible') ) {
        // it's hidden, do something
    }
    else {
        // it's not hidden so do something else
    }
    

    来源::http://www.electrictoolbox.com/jquery-element-is-visible/

    【讨论】:

    • @From.ME.to.YOU:感谢您的回复!我刚刚尝试了您的解决方案,但是当我单击提交按钮时没有任何反应。我不确定,但我认为这与“验证”插件中发生“事件”的时间有关。我想要的只是在错误容器出现时检索它的高度。由于 DOM 已加载,我不明白为什么会有任何问题,但它越来越烦人......
    • 1) 你的#errorMsgContainer 是 div 还是什么? 2)你在加载DOM后运行这段代码吗?
    • 是的,它是一个div,代码在'$(document).ready(function(){})'中。
    • 尝试将css高度添加到该div
    • 我在 css 中添加了 10px。当我单击按钮时,容器显示为 10px 高度,但警告框根本没有显示。就好像它连代码都不读一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 2011-07-10
    • 2018-02-06
    • 2012-05-20
    • 2012-11-05
    • 2019-12-11
    相关资源
    最近更新 更多