在javascript文件中获取选择选项值

时间:2015-04-27 07:56:52

标签: javascript jquery

我的HTML页面中有一个select选项标签,如下所示

<select id="listMails">
   <c:forEach items="${listUsers }" var="users">
      <option value="${users.getEmail() }">${users.getEmail() }</option>                       
    </c:forEach>
</select>

我还有一个JavaScript文件来显示电子邮件列表。我想从select选项标签

中获取电子邮件列表
// Contact select
// ================================
(function() {
    var REGEX_EMAIL = '([a-z0-9!#$%&*+/=?^_`{|}~-]+(?:[a-z0-9!#$%&*+/=?^_`{|}~-]+)*@'
            + '(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)';

    var formatName = function(item) {
        return $.trim((item.firstName || '') + ' '
                + (item.lastName || ''));
    };
    // contact
    $('#selectize-contact')
            .selectize(
                    {

                        persist : false,
                        maxItems : null,
                        valueField : 'email',
                        labelField : 'name',
                        searchField : [ 'firstName',
                                'lastName', 'email' ],
                        sortField : [ {
                            field : 'firstName',
                            direction : 'asc'
                        }, {
                            field : 'lastName',
                            direction : 'asc'
                        } ],
                        options : 
                          //I want to insert the list of                       mails here
                            function() {
                                for(i=0;i<10;i++) {
                                    [{email:'dddddddd',
                                      firstName:'ssss',
                                      lastName:''
                                    }
                                     ]
                                }
                            }
                            [ {
                            email : 'nikola@tesla.com',
                            firstName : 'Nikola',
                            lastName : 'Tesla'
                        }, {
                            email : 'brian@thirdroute.com',
                            firstName : '<%= glksdsd %>Brian',
                            lastName : 'Reavis'
                        }, {
                            email : 'pampersdry@gmail.com',
                            firstName : 'John',
                            lastName : 'Pozy'
                        } ],
                        render : {
                            item : function(item, escape) {
                                var name = formatName(item);
                                return '<div>'
                                        + (name ? '<span class="name">'
                                                + escape(name)
                                                + '</span>'
                                                : '')
                                        + (item.email ? '<small class="text-muted ml10">'
                                                + escape(item.email)
                                                + '</small>'
                                                : '')
                                        + '</div>';
                            },
                            option : function(item, escape) {
                                var name = formatName(item);
                                var label = name || item.email;
                                var caption = name ? item.email
                                        : null;
                                return '<div>'
                                        + '<span class="text-primary">'
                                        + escape(label)
                                        + '</span><br/>'
                                        + (caption ? '<small class="text-muted">'
                                                + escape(caption)
                                                + '</small>'
                                                : '')
                                        + '</div>';
                            }
                        },
                        create : function(input) {
                            if ((new RegExp('^' + REGEX_EMAIL
                                    + '$', 'i')).test(input)) {
                                return {
                                    email : input
                                };
                            }
                            var match = input.match(new RegExp(
                                    '^([^<]*)<' + REGEX_EMAIL
                                            + '>$', 'i'));
                            if (match) {
                                var name = $.trim(match[1]);
                                var postSpace = name
                                        .indexOf(' ');
                                var firstName = name.substring(
                                        0, postSpace);
                                var lastName = name
                                        .substring(postSpace + 1);

                                return {
                                    email : match[2],
                                    firstName : firstName,
                                    lastName : lastName
                                };
                            }
                            return false;
                        }
                    });
})();

提前谢谢大家

1 个答案:

答案 0 :(得分:0)

$("#listMails > option").each(function() {
    alert(this.value);
});