简体   繁体   English

在html表单提交后保留javascript的innerhtml值

[英]keeping a innerhtml value from a javascript after a html form submit

I am using below javascript to collect values from some textboxes,do some calculations and display the result as a innerhtml content 我正在使用下面的javascript从一些文本框中收集值,进行一些计算并将结果显示为innerhtml内容

window.onload = function () {
    var left = document.getElementById('mem_count');
    var right = document.getElementById('siz_single');
    var result = document.getElementById("disp_siz");
    function check(a, b, elem) {

        var txt = '';
        if (a === 0 && b === 0) {

        } 
        else if (a !== 0 && b === 0) {
         txt = "Enter size of single device in above column"

        }
        else if(a == 0 && b !== 0){

        txt = "Enter Meta member count in above column "
        }

        else  {
            var c = 1 +a        
            txt = "Your meta device size is " + (c*b) +" MB" + " = " + (c*b/1024) +" GB ";
        }
disp_siz.innerHTML = txt;

    }

    mem_count.onkeyup = calc;
    siz_single.onkeyup = calc;

    function calc() {
        var a = parseFloat(mem_count.value) || 0;

        var b = parseFloat(siz_single.value) || 0;
        check(a,b, this);
    }
}

and the output will be display in between the div 并且输出将显示在div之间

<div id="disp_siz"><-----above output will come here----></div>

This div is part of a html form. 该div是html表单的一部分。 I am able to keep all my other form values in same field after form submission. 提交表单后,我可以将所有其他表单值保留在同一字段中。 But not able to display above output. 但无法显示以上输出。 It just clearing my values. 这只是清除我的价值观。 Is there anyway I can echo this javascript variable value to the same field after form submision ? 无论如何,在表单细分后,我可以将此JavaScript变量值回显到同一字段吗?

First Option: 第一种选择:

Set it on the serverside. 将其设置在服务器端。

Second Option: 第二种选择:

If the page refreshes, it is like cleaning a whiteboard, you got to start over. 如果页面刷新,就如同清洁白板一样,您必须重新开始。 If the fields are there, trigger the function to run. 如果这些字段在那里,则触发该函数运行。

Add calc(); 添加calc(); to the end of the onload function. onload函数的末尾

    ...
    ...

    function calc() {
        var a = parseFloat(mem_count.value) || 0;

        var b = parseFloat(siz_single.value) || 0;
        check(a,b, this);
    }
    calc();  //<-- added this to trigger the calculation
}

Another problem: 另一个问题:

And you should not reference an element by their id directly. 而且,您不应直接通过其ID引用元素。 You should use 你应该用

document.getElementById("disp_siz").innerHTML = txt;

You're referencing a variable ( disp-siz ) that doesn't exist. 您引用的变量( disp-siz )不存在。 Use the variable you created earlier, result . 使用您先前创建的变量result

result.innerHTML = txt;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM