English 中文(简体)
将窗体变量拖动到一个环行的 Javascript
原标题:Pulling Form Variables through Javascript in a For Loop

我有一个基于用户输入的网页,该网页以含有窗体字段的用户输入模块为基础,如下文所示:

        function Go2() {
            var loans = document.getElementById( count ).value;
            var content = document.getElementById( stage3 ).innerHTML;
            content =  <TABLE Width="100%"> 
                + <TR> 
                + <TD Style="font-weight:bold;" Width="30%">Customer Name</TD> 
                + <TD Style="font-weight:bold;" Width="30%">Customer Number</TD> 
                + <TD Style="font-weight:bold;" Width="30%">Origination Date</TD> 
                + </TR> 
                + </TABLE> ;
            document.getElementById( stage3 ).innerHTML = content;
            for(var i=0; i<loans; i++) {
                content = document.getElementById( stage3 ).innerHTML;
                document.getElementById( stage3 ).innerHTML = content
                    +  <TABLE Width="100%"> 
                    +  <TR> 
                    +  <TD Width="30%"><INPUT Name="CName 
                    + i
                    +  " Size="40" Type="text"></TD> 
                    +  <TD Width="30%"><INPUT Name="CNumber 
                    + i
                    +  " Size="40" Type="text"></TD> 
                    +  <TD Width="30%"><INPUT Name="Date 
                    + i
                    +  " Size="40" Type="text"></TD> 
                    +  </TR> 
                    +  </TABLE> ;
            }
            content = document.getElementById( stage3 ).innerHTML;
            document.getElementById( stage3 ).innerHTML = content
                    +  <TABLE><TR><TD><INPUT Type="Button" Value="Submit" onClick="Go3()"></TD></TR></TABLE> ;
        }

现在我需要做的就是通过表单循环 并抽出每个表单字段的值。这是我得到的:

for (var n=0; n<loans; n++) {
content +=  <TR> 
    +  <TD Colspan="2"> 
    + document.getElementById( CName + n ).value
    +  </TD> 
    +  <TD Colspan="2"> 
    + document.getElementById( CNumber + n ).value
    +  </TD> 
    +  <TD> 
    + document.getElementById( Date + n ).value
    +  </TD> 
    +  </TR> ;
}

我最后的显著进展是 吐出“null”的“null”字,这根本不算什么进步。我看过Eval, 但有不少警告要警告它。

有什么想法吗?

最佳回答

我想你想

document.getElementById( CName  + n).value

(n在引号之外)

问题回答

应该是CName + n - 也就是说,你找错地方了

如果您所有的控件都以窗体形式存在,您可以访问这些控件,具体如下:

var allControls = document.<formId>.elements;

var allControls = document.forms[formId].elements;

Then iterate over the list of controls to get the values. The form controls must have names to be successful, no need f或IDs. You can also get the values as:

var value = allControls[ CName  + n].value;

或just

var form = docment.forms[formID];
var value = form[ CName  + n].value;




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签