【问题标题】:jQuery - Multiple Email Addresses in Input FieldjQuery - 输入字段中的多个电子邮件地址
【发布时间】:2015-09-30 08:12:24
【问题描述】:

我正在开发一种允许用户在输入字段中输入多个电子邮件地址的表单。

通过在网上搜索,我发现了这个 jQuery 插件: http://t2a.co/blog/index.php/multiple-value-input-field-with-jquery/

(function( $ ){

     $.fn.multipleInput = function() {

          return this.each(function() {

               // create html elements

               // list of email addresses as unordered list
               $list = $('<ul />');

               // input
               var $input = $('<input type="text" />').keyup(function(event) {

                    if(event.which == 32 || event.which == 188) {
                         // key press is space or comma
                         var val = $(this).val().slice(0, -1); // remove space/comma from value

                         // append to list of emails with remove button
                         $list.append($('<li class="multipleInput-email"><span> ' + val + '</span></li>')
                              .append($('<a href="#" class="multipleInput-close" title="Remove" />')
                                   .click(function(e) {
                                        $(this).parent().remove();
                                        e.preventDefault();
                                   })
                              )
                         );
                         $(this).attr('placeholder', '');
                         // empty input
                         $(this).val('');
                    }

               });

               // container div
               var $container = $('<div class="multipleInput-container" />').click(function() {
                    $input.focus();
               });

               // insert elements into DOM
               $container.append($list).append($input).insertAfter($(this));

               // add onsubmit handler to parent form to copy emails into original input as csv before submitting
               var $orig = $(this);
               $(this).closest('form').submit(function(e) {

                    var emails = new Array();
                    $('.multipleInput-email span').each(function() {
                         emails.push($(this).html());
                    });
                    emails.push($input.val());

                    $orig.val(emails.join());

               });

               return $(this).hide();

          });

     };
})( jQuery );

我安装了插件,下面是我的结果截图:

我的问题:

我们如何编辑插件以确保最后一个值(电子邮件地址)的末尾没有逗号?

这是一个小提琴:https://jsfiddle.net/William3780/oL14dgqp/

这里是我正在处理的实时安装的链接:http://51146.com/free/refer-a-friend/ *请填写“朋友的电子邮件”字段并提交表单以查看问题。

非常感谢您的意见,谢谢。

【问题讨论】:

  • JsFiddle 会很有用。
  • 第一张图片中有类似tag 的内容。那么当你得到这个,分离的值回到它的形式?由于我创建了一个 demo,我可以看到,一旦您单击 ,,您就会得到类似 UI 的标签,但我没有找到其他选项可以将其恢复为 , 分隔值..
  • 问题应该是emails.push($input.val());。它将一个空值推送到数组中,最好检查input.val() != ""然后将其推送到数组中。
  • 你可能会看到这个例子。 jsfiddle.net/h3g3gLf8 可以清楚的看到它把一个空字符串压入了数组中。
  • 使用 jsFiddle 和实时安装链接更新了问题。

标签: javascript jquery html


【解决方案1】:

很好地改变了您的代码标准,但创建了一个有效的多个电子邮件输入。这是代码

HTML

<div class="multiple-val-input">
    <ul>
        <input type="text">
        <span class="input_hidden"></span>
    </ul>
</div>

CSS

.multiple-val-input{
        height: auto;
        min-height: 34px;
        cursor: text;
    }
    .multiple-val-input ul{
        float: left;
        padding: 0;
        margin: 0;
    }
    .multiple-val-input ul li{
        list-style: none;
        float: left;
        padding: 3px 5px 3px 5px;
        margin-bottom: 3px;
        margin-right: 3px;
        position: relative;
        line-height: 13px;
        cursor: default;
        border: 1px solid #aaaaaa;
        border-radius: 3px;
        -webkit-box-shadow: 0 0 2px #fff inset, 0 1px 0 rgba(0, 0, 0, 0.05);
        box-shadow: 0 0 2px #fff inset, 0 1px 0 rgba(0, 0, 0, 0.05);
        background-clip: padding-box;
        -webkit-touch-callout: none;
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
        background-color: #e4e4e4;
        background-image: -webkit-gradient(linear, 0% 0%, 0% 100%, color-stop(20%, #f4f4f4), color-stop(50%, #f0f0f0), color-stop(52%, #e8e8e8), color-stop(100%, #eee));
        background-image: -webkit-linear-gradient(top, #f4f4f4 20%, #f0f0f0 50%, #e8e8e8 52%, #eee 100%);
        background-image: -moz-linear-gradient(top, #f4f4f4 20%, #f0f0f0 50%, #e8e8e8 52%, #eee 100%);
        background-image: linear-gradient(to top, #f4f4f4 20%, #f0f0f0 50%, #e8e8e8 52%, #eee 100%);
    }
    .multiple-val-input ul li a{
        display: inline;
        color: #333;
        text-decoration: none;
    }
    .multiple-val-input ul li a, .multiple-val-input ul li div{
        display: inline;
        margin-left: 3px;
    }
    .multiple-val-input input[type="text"]{
        float: left;
        border: none;
        outline: none;
        height: 20px;
        min-width: 5px;
        width: 5px;
    }
    .multiple-val-input span.input_hidden{
        font-size: 14px;
        position: absolute;
        clip: rect(0,0,0,0);
    }

JQUERY

$('.multiple-val-input').on('click', function(){
            $(this).find('input:text').focus();
        });
        $('.multiple-val-input ul input:text').on('input propertychange', function(){
            $(this).siblings('span.input_hidden').text($(this).val());
            var inputWidth = $(this).siblings('span.input_hidden').width();
            $(this).width(inputWidth);
        });
        $('.multiple-val-input ul input:text').on('keypress', function(event){
            if(event.which == 32 || event.which == 44){
                var toAppend = $(this).val();
                if(toAppend!=''){
                    $('<li><a href="#">×</a><div>'+toAppend+'</div></li>').insertBefore($(this));
                    $(this).val('');
                } else {
                    return false;
                }
                return false;
            };
        });
        $(document).on('click','.multiple-val-input ul li a', function(e){
            e.preventDefault();
            $(this).parents('li').remove();
        });

【讨论】:

    【解决方案2】:

    JSFiddle 将不胜感激,但如果您尝试删除数组或字符串中项目数组的最后一个元素的逗号。我可以想象几种方法:

    1) 正则表达式

    var lastCommaPatter = ",$";
    
    var emails = "test@gmail.com, test2@gmail.com, test@gmail.com,";
    
    email.replace(lastCommaPatter, "");
    

    2) 将整个事物覆盖到一个集合中并删除最后一个空白元素

    var SEPARATOR = ",";
    
    var emails = "test@gmail.com, test2@gmail.com, test@gmail.com,";
    
    var emailsCollection = email.split(SEPARATOR);
    
    var collectionSize = emailsCollection.length;
    
    if(collectionSize > 0){
    
      array.splice(emailsCollection, collectionSize - 1);
    
    }
    

    【讨论】:

    • 感谢您的反馈,您能告诉我如何将您的建议实施到插件中吗?
    • 您应该告诉我您希望我的代码示例存在的特定代码部分。否则我不能轻易指出你必须在你的源代码中实现这个。
    猜你喜欢
    • 2010-12-04
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 2021-06-07
    • 1970-01-01
    • 2011-03-31
    相关资源
    最近更新 更多