如何循环遍历多个字段集?

时间:2012-04-25 01:12:49

标签: javascript jquery html forms fieldset

我有这个功能附加到我的表格(这是一个模态窗口):

function appendAddressForm(name, line1, line2, phone, fax) {
    $("#addressForm form").append(
        "<div style='clear:both;text-align:left;padding:10;'>" +        
        "<fieldset style='width:70%;padding:5;float:left'>" +
        "<ul class='ui-helper-clearfix'>" +
            "<li class='ui-state-default ui-corner-all' title='Delete Address' id><span class='ui-icon ui-icon-minus'></span></li>" +               
        "</ul>" +           
        "<label for='addressName'  style='width:100px'>Office Name</label>" +
        "<input type='text' size='20' name='addressName' id='addressName' value='" +  name + "' /> <br>" +
        "<label for='addressOne'  style='width:100px'>Address Line 1</label>" +
        "<input type='text' size='40' name='addressOne' id='addressOne' value='" +  line1 + "' /> <br>" +
        "<label for='addressTwo'  style='width:100px'>Address Line 2</label>" +
        "<input type='text' size='40' name='addressTwo' id='addressTwo' value='" +  line2 + "'/> <br>" +
        "<label for='addressPhone'  style='width:100px'>Phone</label>" +
        "<input type='text' size='20' name='addressPhone' id='addressPhone' value='(P)" +  phone + "' /> <br>" +
        "<label for='addressFax'  style='width:100px'>Fax</label>" +
        "<input type='text' size='20' name='addressFax' id='addressFax' value='(F)" +  fax + "' />" +   
    "</fieldset>" +             
"</div>");
}

这就是我访问第一个字段集的方式

var addressName = $.trim($("#addressName").val());
var addressOne = $.trim($("#addressOne").val());
var addressTwo = $.trim($("#addressTwo").val());
var addressPhone = $.trim($("#addressPhone").val());
var addressFax = $.trim($("#addressFax").val());

if(addressName!=“”&amp;&amp; addressOne!=“”&amp;&amp; addressPhone!=“(P)”&amp;&amp; addressFax!=“(F)”)

{

我有jQuery和一个按钮。单击此按钮后,我必须附加包含此字段集的部分。我可以在表单中添加最多6个字段集。我的问题是,我只能获得第一个字段集的值。我也要验证所有其他字段集。如何从其他字段集中获取值?

1 个答案:

答案 0 :(得分:1)

问题在于,您的所有输入都具有相同的名称属性和ID,在此appendAddressForm(name, line1, line2, phone, fax, number)中添加了另一个变量,我添加了number。将此变量添加到您的id和名称属性,即:

<input type='text' size='20' name='addressName-"+number+"' id='addressName-"+number+"' value='" +  name + "' />