如何获取隐藏或自动生成的内容的价值?

发布于 2024-09-14 00:23:42 字数 2207 浏览 2 评论 0原文

这个问题是在解决我的上一个问题之后提出的,我想从隐藏表单中获取一些值,但是当我尝试检索它们时仅出现空字符串,我考虑过仅使用数组来存储引入的信息,但我想知道是否之后就可以检索它以及如何检索它。

另外,有一个用一些 javascript 动态生成的表:

function createTable(){
        if ( document.getElementById("invoiceFormat").rowNumber.value != ""){
            rows = document.getElementById("invoiceFormat").rowNumber.value;
        }

        var contents = "<table id='mt'><tr>";

        if ( document.getElementById("invoiceFormat").cb1[0].checked ){
            contents = contents + "<td class='htd'>Quantity</td>";
        }if (document.getElementById("invoiceFormat").cb1[1].checked ){ 
            contents = contents + "<td class='htd'>Description</td>";
        }if (document.getElementById("invoiceFormat").cb1[2].checked ){ 
            contents = contents + "<td class='htd'>Unitary Price</td>";
        }if (document.getElementById("invoiceFormat").cb1[3].checked ){ 
            contents = contents + "<td class='htd'>Subtotal</td>";
        }

        for (i=4; i<=k; i++){
            if (document.getElementById("invoiceFormat").cb1[i].checked ){
                contents = contents + "<td>" + document.getElementById("invoiceFormat").cb1[i].value + "</td>";
            }
        }


        contents = contents + "</tr>";

        for (j=1; j<=rows; j++){
            contents = contents + "<tr>";
            for (l=0; l<=k; l++){
                if (document.getElementById("invoiceFormat").cb1[l].checked ){
                hotfix = l +1;
                contents = contents + "<td> <input id='cell" + j + "_" + hotfix + "' name='cell' type='text' size='15' /> </td>";
                }
            }
            contents = contents + "</tr>";
        }

        contents = contents + "</table>";
        var createdTable = document.getElementById("mainTable");
        createdTable.innerHTML = contents;  

    }

创建后我尝试访问它,但到目前为止还没有运气,我只是无法获取用户在创建的输入字段中输入的内容。我该怎么做?

我正在使用原始 javascript 和 jQuery,所以欢迎使用或不使用库的答案:)

This question comes after solving my last question, I'd like to get some values out of the hidden forms but when I try to retrieve them only empty strings come by, I've considered just using arrays to store the information as it is introduced but I'd like to know if it's possible just to retrieve it afterwards and how.

Also, There is a table that is generated on the fly with some javascript:

function createTable(){
        if ( document.getElementById("invoiceFormat").rowNumber.value != ""){
            rows = document.getElementById("invoiceFormat").rowNumber.value;
        }

        var contents = "<table id='mt'><tr>";

        if ( document.getElementById("invoiceFormat").cb1[0].checked ){
            contents = contents + "<td class='htd'>Quantity</td>";
        }if (document.getElementById("invoiceFormat").cb1[1].checked ){ 
            contents = contents + "<td class='htd'>Description</td>";
        }if (document.getElementById("invoiceFormat").cb1[2].checked ){ 
            contents = contents + "<td class='htd'>Unitary Price</td>";
        }if (document.getElementById("invoiceFormat").cb1[3].checked ){ 
            contents = contents + "<td class='htd'>Subtotal</td>";
        }

        for (i=4; i<=k; i++){
            if (document.getElementById("invoiceFormat").cb1[i].checked ){
                contents = contents + "<td>" + document.getElementById("invoiceFormat").cb1[i].value + "</td>";
            }
        }


        contents = contents + "</tr>";

        for (j=1; j<=rows; j++){
            contents = contents + "<tr>";
            for (l=0; l<=k; l++){
                if (document.getElementById("invoiceFormat").cb1[l].checked ){
                hotfix = l +1;
                contents = contents + "<td> <input id='cell" + j + "_" + hotfix + "' name='cell' type='text' size='15' /> </td>";
                }
            }
            contents = contents + "</tr>";
        }

        contents = contents + "</table>";
        var createdTable = document.getElementById("mainTable");
        createdTable.innerHTML = contents;  

    }

After it's created I've tried to access it but without luck so far, I just can't get what the user inputs in the input fields that are created. How can I do this?

I'm using raw javascript with jQuery so answers with or without the library are welcomed :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦断已成空 2024-09-21 00:23:42

document.getElementById("invoiceFormat").cb1[3].checked
首先,我不知道“.cb1[3]”在这里意味着什么,所以我会忽略它并告诉你如何解决这个问题:(我假设“invoiceFormat”是你的表单的id。)
1)在您的表单中设置您拥有的每个字段的名称属性。这样您就可以像 document.getElementById("invoiceFormat").fieldName.value 一样访问它们,

如果您将使用此方法,请确保将表单放入本地变量中。它会快很多

var form = document.getElementById("invoiceFormat");
form.fieldName.value;

2) 给每个字段一个唯一的 id,然后直接在字段上而不是在表单上使用 getElementById 。
我不起诉这种方法是否更好,但我一直在使用第二种方法。我想我只是习惯了。

3)还有一种方法,但可能有点矫枉过正。当您创建表单字段时,您可以将它们放入一个对象中(不是值,而是元素本身),甚至将其隐藏在 关闭。这样你就可以调用类似的东西

formElements.formFieldOne.value;

document.getElementById("invoiceFormat").cb1[3].checked
First of all I do not know what ".cb1[3]" means here so I will ignore it and will tell you how I would solve this problem: (I assume that "invoiceFormat" is id of your form.)
1) in your form set name property of every field you have. that way you can reach them like document.getElementById("invoiceFormat").fieldName.value

if you will use this method make sure that put your form in a local variable. it will be a lot faster

var form = document.getElementById("invoiceFormat");
form.fieldName.value;

2) give every field you have a unique id and just use getElementById directly on fields not on form.
I am not sue if this method is better, but I am useing second one all the time. I just get used to it I guess.

3) there is one more way but it might be an overkill. when you create your form fields, you can put them in an object(not values but the elements themselves) and even hide it in a closure. That way you can call something like

formElements.formFieldOne.value;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文