简体   繁体   English

在javascript中点击“提交”按钮后,如何获取表格div +错误消息的高度?

[英]How do you get the height of a form div + error messages after hitting the submit button in javascript?

So, I've been trying to get this form to work (css and javascript), but I'm stuck on something: i have the form, and everything's basically working, excpet that i have a container for the form div: formbody, and a container for the submit, clear, etc. The top div is set to height:auto; 因此,我一直在尝试使此表单正常工作(css和javascript),但我遇到了一些麻烦:我拥有表单,并且基本上一切正常,例如,我为div表单提供了一个容器:formbody,以及用于提交,清除等的容器。top div设置为height:auto; position:absolute; 位置:绝对; and the bottom is set to, nothing. 底部设置为“无”。 it just had a width. 它只有一个宽度。

When the user clicks on the submit button, the formbody will need to resize, but i don't know how to get the new size of the form in order to set the position of the bottom div. 当用户单击提交按钮时,表单主体将需要调整大小,但是我不知道如何获取表单的新大小以设置底部div的位置。

I just added more of the css - there's a background div that just holds the template open for the form -- I had that set to relative -- but formbody has a position absolute because the height needs to be auto in order to resize for the errors (and when i set it to auto without position:absolute, formbody shrank to 20px). 我刚刚添加了更多的CSS-有一个背景div只能将表单的模板保持打开状态-我已将其设置为relative-但formbody的位置是绝对的,因为高度需要自动设置才能调整大小错误(当我将其设置为不带位置的自动:绝对时,formbody缩小为20px)。

    .background {
    width: 0px;
    height: 700px;
    position: relative;
    z-index:999;
    }

.formbody {
    background-image: url('');
    background-repeat: no-repeat;
    background-position: left bottom;
    position:absolute;
    left:0px;
    top:50px;
    width:700px;
    height:auto;
    padding-bottom:20px;
    border:1px solid #d2d2d2;
}

.bottom {
    width:700px;
}

something like this 像这样的东西

<script>

    var formbody_width= $(".formbody").width();

    $(".submit").live("click", function) {

       $(".formbody").css("height", formbody_width + 300 + "px");     

    }

</script>

Would this work? 这行得通吗? (Here's a live preview: http://jsfiddle.net/rcMGn/1/ ) (这是实时预览: http : //jsfiddle.net/rcMGn/1/

Basically what I'm doing here is getting the first node with class formwidth (as per your css) and using the DOM model to acquire its style properties. 基本上,我在这里要做的是获取具有类formwidth的第一个节点(根据您的css),并使用DOM模型获取其样式属性。

The function getElementsByClass will return an array of all elements in the document with class formwidth (and I'm supposing there's going to be only one) and getting the first element from the array (which is supposed to be 1) and then styling it. 函数getElementsByClass将返回文档中具有class formwidth类的所有元素的数组(我假设只有一个),并从该数组中获取第一个元素(假定为1),然后对其进行样式设置。

You don't need to care about the function... To get the width just the first line of the code below... 您不需要关心函数...要获得宽度,只需下面代码的第一行...

formWidth = getElementsByClass('formbody',null,'form')[0].offsetWidth;

To get the height, use this one: 要获得高度,请使用以下一项:

formHeight = getElementsByClass('formbody',null,'form')[0].offsetHeight;

and copy the function from the files of Dustin Diaz , the function below... 并从Dustin Diaz的文件中复制该功能,以下功能...

function getElementsByClass(searchClass,node,tag) {
    var classElements = new Array();
    if ( node == null )
        node = document;
    if ( tag == null )
        tag = '*';
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
    for (i = 0, j = 0; i < elsLen; i++) {
        if ( pattern.test(els[i].className) ) {
            classElements[j] = els[i];
            j++;
        }
    }

    return classElements;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 点击表单中的提交按钮后如何显示隐藏的div? - How to show hidden div after hitting submit button in form? 使用Javascript按下“提交”按钮后如何访问复选框表单值? - How to access checkbox form values after hitting the submit button with Javascript? 在 React 中隐藏提交按钮后如何提交表单 - How do you submit a form after hiding the submit button in React 你如何使用javascript提交表单<input type=“button”> - How do you submit a form with javascript with a <input type=“button”> 你如何在纯 javascript 中获得 div 高度? - How do you get div height in pure javascript? 延迟后提交表单数据,点击提交按钮? - Submit Form-data after delay, on hitting a submit-button? 如何区分单击提交按钮和输入输入以提交表单? - How do I differentiate between clicking a submit button and hitting enter in an input to submit a form? 从javascript提交表单后,如何通过提交按钮保持php $ _GET值? - How to keep php $_GET value from a submit button after form submit from javascript? 在 TinyMCE 中的一个 div 之后点击 [return] 会创建另一个 div。 你知道为什么以及如何阻止它吗? - Hitting [return] after a div in TinyMCE creates another div. Do you know why and how to stop it? 表单到Javascript,无需点击提交 - Form to Javascript without hitting submit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM