【问题标题】:click checkbox dynamically and add the email address in list and remove duplicate email address using javascript动态单击复选框并在列表中添加电子邮件地址并使用 javascript 删除重复的电子邮件地址
【发布时间】:2017-02-12 05:42:00
【问题描述】:

文件名:contact.jsp

${contactList} 拥有多个联系人。

  1. 如果复选框被选中,那么它将被添加到 emailList 类,如果复选框未被选中,那么电子邮件地址将从列表中删除。
  2. 从列表中删除重复的电子邮件地址。
  3. 例如:kumar@gmail.com、sam@yahoo.com - 在电子邮件地址的中间放逗号
  4. 最终将所有电子邮件地址分配给父页面 ID $("#toAddress")
<c:forEach items="${contactList}" var="contact">             
    <cong:td>
        <input type="checkbox" name="selectContact"  id="selectContact" class="emailList" onclick="addEmailinList(${contact.email});"/>
    </cong:td>
    <cong:td>${contact.accountNo}</cong:td>
    <cong:td>${contact.firstName}&nbsp;&nbsp;${contact.lastName}</cong:td>
    <cong:td>${contact.position}</cong:td>
    <cong:td>${contact.email}</cong:td>
    <cong:td>${contact.phone}</cong:td>
    <cong:td>${contact.fax}</cong:td>
</c:forEach>

       <cong:td>
         <input type="button" value="Submit" class="button-style1"  style="width:50px;" onclick="definepls();"/>
       </cong:td>

文件名:contact.js

function addEmailinList(ele) {
    var mailList = [];
    $(".emailList:checked").each(function () {
        alert(ele);            //  here i got email address.
        mailList.push(ele);
    });
    parent.$("#toAddress").val($(".emailList").val());
}

【问题讨论】:

  • 我忘了指定提交按钮。单击提交按钮时,所有选中的电子邮件地址都将填充父页面 id $("#toAddress")。
  • 是的。一旦他们提交,它将携带所有检查的电子邮件地址到父页面 id $("#toAddress")。

标签: javascript jquery jsp checkbox jsp-tags


【解决方案1】:

要将所有已检查的电子邮件填充到$('#toAddress')你可以这样做:

  1. 删除onclick="addEmailinList(${contact.email});" 并添加data-email="${contact.email}" 以引用所有复选框输入字段中的email
  2. 在所有 $('input.emailList') 上设置一个 jQuery change 事件监听器

查看文件:

<c:forEach items="${contactList}" var="contact">
    <cong:td>
        <input type="checkbox" name="selectContact" id="selectContact" class="emailList" data-email="${contact.email}">
    </cong:td>
    <cong:td>${contact.accountNo}</cong:td>
    <cong:td>${contact.firstName}&nbsp;&nbsp;${contact.lastName}</cong:td>
    <cong:td>${contact.position}</cong:td>
    <cong:td>${contact.email}</cong:td>
    <cong:td>${contact.phone}</cong:td>
    <cong:td>${contact.fax}</cong:td>
</c:forEach>

<cong:td>
    <input type="button" value="Submit" class="button-style1" style="width:50px;" onclick="definepls()" />
</cong:td>

JavaScript 文件:

$('input.emailList').on('change', function () {
    var $this = $(this),
        $toAddress = $('#toAddress'),
        email = $this.data('email'),
        mailList = ($toAddress.text() !== '') ? $toAddress.text().split(', ') : [];

    if ($this.is(':checked')) {
        // Add email to the list
        mailList.push(email);
    } else {
        // Remove email from the list
        for (var i = mailList.length - 1; i >= 0; i--) {
            if (mailList[i] === email) {
                mailList.splice(i, 1);
                break;
            }
        }
    }

    // Populate #toAddress 
    $toAddress.html(mailList.join(', '));
});

【讨论】:

  • 很好帮助..如果作品将其标记为正确答案,请
猜你喜欢
  • 2019-12-17
  • 1970-01-01
  • 2017-08-29
  • 2017-07-23
  • 2010-09-19
  • 2021-08-29
  • 2010-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多