如何将具有顺序但不同名称的元素用于数组?

时间:2018-03-26 22:13:00

标签: javascript jquery

我不确定标题是否准确。我仍然熟悉jQuery / JavaScript,所以请随时纠正我的语法/术语。

问题:我有一个表单页面,当用户点击发送时,我需要阅读并验证用户提供的数据。我遇到问题的数据是联系人数据。默认情况下,表单会列出两个联系人有一个按钮,允许用户动态地一次添加一个新的联系人到页面。因此,我的代码可能需要在此页面上验证数十或数百个联系人。我无法更改表单的代码/格式。我只能添加Javascript / jQuery来验证数据。

每个联系人都在重复包装中设置,如下所示:

<div class="ff-sec-repeat-wrapper">
    <div class="ff-item-row">
        <div class="ff-col-1 ff-label-col">LABELS&STUFF</div>
        <div class="ff-col-2 ff-field-col">
            <input type="textbox"
             id="Membership_Application__c.Contact.A_1_.FirstName" 
             placeholder=""
             name="Membership_Application__c.Contact.A_1_.FirstName" 
             vatt="STRING"
             class="ff-input-type ff-type-text"
             data-maxlengthmessage="Maximum 40 characters" 
             maxlength="40" 
             value=""
             data-requiredmessage="required"
             data-isupsert="false"
             data-ishidden="false" 
             data-vatt="STRING">
        </div>
    </div>
    <div class="ff-item-row">
        <div class="ff-col-1 ff-label-col">LAST_NAME_STUFF_SAME_FORMAT_AS ABOVE</div>
        <div class="ff-col-2 ff-label-col">...</div>
    </div>
    <div class="ff-item-row">
        <div class="ff-col-1 ff-label-col">EMAIL_STUFF_SAME_FORMAT_AS_ABOVE</div>
        <div class="ff-col-2 ff-field-col">
            <input type="textbox"
            id="Membership_Application__c.Contact.A_1_.Email"
            placeholder="" 
            name="Membership_Application__c.Contact.A_1_.Email"
            vatt="EMAIL" 
            class="ff-input-type ff-type-text" 
            data-maxlengthmessage="Maximum 80 characters" 
            maxlength="80" 
            value="" 
            data-requiredmessage="required" 
            data-isupsert="false" 
            data-ishidden="false" 
            data-vatt="EMAIL">
        </div>
    </div>
</div>    

<div class="ff-sec-repeat-wrapper">
    <div class="ff-item-row">
        <div class="ff-col-1 ff-label-col">LABELS&STUFF</div>
        <div class="ff-col-2 ff-field-col">
            <input type="textbox"
             id="Membership_Application__c.Contact.A_2_.FirstName" 
             placeholder=""
             name="Membership_Application__c.Contact.A_2_.FirstName" 
             vatt="STRING"
             class="ff-input-type ff-type-text"
             data-maxlengthmessage="Maximum 40 characters" 
             maxlength="40" 
             value=""
             data-requiredmessage="required"
             data-isupsert="false"
             data-ishidden="false" 
             data-vatt="STRING">
        </div>
    </div>
    <div class="ff-item-row">
        <div class="ff-col-1 ff-label-col">LAST_NAME_STUFF_SAME_FORMAT_AS ABOVE</div>
        <div class="ff-col-2 ff-label-col">...</div>
    </div>
    <div class="ff-item-row">
        <div class="ff-col-1 ff-label-col">EMAIL_STUFF_SAME_FORMAT_AS_ABOVE</div>
        <div class="ff-col-2 ff-field-col">
            <input type="textbox"
            id="Membership_Application__c.Contact.A_2_.Email"
            placeholder="" 
            name="Membership_Application__c.Contact.A_2_.Email"
            vatt="EMAIL" 
            class="ff-input-type ff-type-text" 
            data-maxlengthmessage="Maximum 80 characters" 
            maxlength="80" 
            value="" 
            data-requiredmessage="required" 
            data-isupsert="false" 
            data-ishidden="false" 
            data-vatt="EMAIL">
        </div>
    </div>
</div>    

以上是基本设置。每个重复包装器包含多行具有不同联系人的数据。每个联系人都有许多字段,例如LastName,EmailAddress,ContactRole等。但我怀疑,一旦我理解了如何访问其中一个,其他人将基本上以相同的方式访问。

重要的是(我相信):每个新的联系人ID都是使用Alpha_Number_组合顺序迭代的。因此,在上面,第一个联系人的FirstName ID是Application__c.Contact.A_1_.FirstName,而eigth联系人的标识是Application__c.Contact.A_8_.FirstName。此数字会更改为LastNameEmailAddress等所有其他联系人字段。

至少,如何检索所有联系人&#39;电子邮件地址成阵列?这样我就可以检查重复的电子邮件地址,并将提供的电子邮件地址与我已经检索过的其他数据相匹配。

1 个答案:

答案 0 :(得分:1)

因此,您正在尝试弄清楚如何验证以下内容,而无需手动为&#34;联系人1,联系人2,...,联系人8&#34;等进行验证?< / p>

使用querySelectorAll将是您的朋友。我们可以使用它来获取所有重复包装并将它们添加到数组中,然后您可以循环遍历该数组以进行验证。

这是一个简单的例子。出于演示目的,它将触发虚假名称并将两个有效名称添加到对象。当然,您可以使用自己的验证以及从那里做的任何事情来替换它。我认为你实际上正在寻找的是一种循环不确定数量的领域的方法;这

querySelectorAll

和一个简单的循环:

for( i = START; i <= NUM_OF_ITEMS; i++ ){

将简单的工作。请查看以下代码段:

&#13;
&#13;
// Run this code when #myform is submitted
document.querySelector('#myform').addEventListener("submit", function(e){
  e.preventDefault(); // Prevent page load for demo purposes

  var object = {},
      valid  = 0;

  // Grab an array of all the repeat-wrapper fields
  var fields = document.querySelectorAll('.ff-sec-repeat-wrapper');

  // Loop through that array, it will stop when total # of `repeat-wrappers` has been reached
  for( i = 1; i <= fields.length; i++ ){
    // The "magic" here is the '+ i +', which is replaced with the current iteration number
    // Since i will never be greater than fields.length, it will only run as much as long as needed
    FirstName = document.getElementById('Membership_Application__c.Contact.A_'+ i +'_.FirstName').value;
    LastName  = document.getElementById('Membership_Application__c.Contact.A_'+ i +'_.LastName').value;

  //Example Fields:
  //Email     = document.getElementById('Membership_Application__c.Contact.A_'+ i +'_.Email').value;
  //SomeField = document.getElementById('Membership_Application__c.Contact.A_'+ i +'_.SomeField').value;

    contact = []; // For demo purposes we're making an associative array of valid contacts

    // Put your validation here
    // Demo validation will only kick out the fake name "Foo Bar"
    if( FirstName != 'Foo' ) contact.push( FirstName );
    if( LastName != 'Bar' )  contact.push( LastName );

    // If contact is valid, add it to our object
    if( contact.length > 0 ) object[valid++] = contact;
  }

  console.log( object ); // Will contain 0:John, 1:Jane - having skipped `Foo` for not meeting out validation
});
&#13;
<form id="myform">
  <div class="ff-sec-repeat-wrapper">
      <div class="ff-item-row">
          <div class="ff-col-2 ff-field-col">First
              <input type="text" id="Membership_Application__c.Contact.A_1_.FirstName" value="John"/>
          </div>
          <div class="ff-col-2 ff-field-col">Last
              <input type="text" id="Membership_Application__c.Contact.A_1_.LastName" value="Doe" />
          </div>
     </div>
  </div>
  <div class="ff-sec-repeat-wrapper">
      <div class="ff-item-row">
          <div class="ff-col-2 ff-field-col">First
              <input type="text" id="Membership_Application__c.Contact.A_2_.FirstName" value="Foo" />
          </div>
          <div class="ff-col-2 ff-field-col">Last
              <input type="text" id="Membership_Application__c.Contact.A_2_.LastName" value="Bar" />
          </div>
     </div>
  </div>
  <div class="ff-sec-repeat-wrapper">
      <div class="ff-item-row">
          <div class="ff-col-2 ff-field-col">First
              <input type="text" id="Membership_Application__c.Contact.A_3_.FirstName" value="Jane" />
          </div>
          <div class="ff-col-2 ff-field-col">Last
              <input type="text" id="Membership_Application__c.Contact.A_3_.LastName" value="Smith" />
          </div>
     </div>
  </div>
  <input type="submit" />
</form>
&#13;
&#13;
&#13;