1

我试图阻止用户使用 jQuery 使用 KeyPress() 输入空格“”。它在除 IE8 之外的所有浏览器上都可以正常工作。

这是我的代码:

$("<input type='text'>")
.attr( 'placeholder', opts.addTagPrompt )
.keypress( function(e) {
var $this = $(this),
    pressed = e.which;

for ( i in opts.delimiters ) {

    if (pressed == opts.delimiters[i]) {
        if($this.val().indexOf(";",0) > 0)
        {
            var list = $this.val().split(';');
        }else{
            var list = $this.val().split(',');
        }

        for(var i in list){
            self.add( list[i] );
        }

        e.preventDefault(); 
        return false;
    }
}
})

更新1:

如果用户单击空格或 Enter 或逗号,它是一个 tagify 插件,它应该为所选输入返回 false 和机会 Css。

(function ($) {

    $.widget("ui.tagify", {
        options: {
            delimiters: [13, 32, 44],          // what user can type to complete a tag in char codes: [enter], [space], [comma]
            outputDelimiter: ',',           // delimiter for tags in original input field
            cssClass: 'tagify-container',   // CSS class to style the tagify div and tags, see stylesheet
            addTagPrompt: 'enter an email address'        // placeholder text
        },

        _create: function() {
            var self = this,
                el = self.element,
                opts = self.options;

            this.tags = [];
            var ctrlDown = false;
            // hide text field and replace with a div that contains it's own input field for entering tags
            this.tagInput = $("<input type='text'>")
                .attr( 'placeholder', opts.addTagPrompt )
                .keypress( function(e) {
                    var $this = $(this),
                        pressed = e.which;

                    for ( i in opts.delimiters ) {

                        if (pressed == opts.delimiters[i]) {
                            if($this.val().indexOf(";",0) > 0)
                            {
                                var list = $this.val().split(';');
                            }else{
                                var list = $this.val().split(',');
                            }

                            for(var i in list){
                                self.add( list[i] );
                            }

                            e.preventDefault(); 
                            return false;
                        }
                    }
                })

                // Ctrl+V Detection
                .keydown(function(e)
                {
                    if (e.keyCode == 17) ctrlDown = true;
                })
                .keyup(function(e)
                {
                    if (e.keyCode == 17) ctrlDown = false;
                })
                .keyup(function(e)
                {
                    var $this = $(this);
                    console.log($this.val());
                    if (ctrlDown && (e.keyCode == 86) ) {
                        if($this.val().indexOf(";",0) > 0)
                        {
                            var list = $this.val().split(';');
                        }else{
                            var list = $this.val().split(',');
                        }
                        var name_fix = "";
                        for(var i in list){
                            console.log(list[i]);
                            if(!checkEmail(list[i])){
                                name_fix = list[i]+", ";
                            }else{
                                if (name_fix != "") { 
                                    self.add( name_fix+list[i] );   
                                    name_fix = "";
                                }else{  
                                    self.add( list[i] );    
                                }
                            }



                        }

                        e.preventDefault(); 
                        return false;
                    }
                })



                .keydown( function(e) {
                    var $this = $(this),
                        pressed = e.which;

                    // if backspace is hit with no input, remove the last tag
                    if (pressed == 8) { // backspace
                        if ( $this.val() == "" )
                        {
                            self.remove();
                            return false;
                        }
                        return;
                    }
                });

            this.tagDiv = $("<div></div>")
                .addClass( opts.cssClass )
                .click( function() {
                    $(this).children('input').focus();
                })
                .append( this.tagInput )
                .insertAfter( el.hide() );

            // if the field isn't empty, parse the field for tags, and prepopulate existing tags
            var initVal = $.trim( el.val() );

            if ( initVal ) {
                var initTags = initVal.split( opts.outputDelimiter );
                $.each( initTags, function(i, tag) {
                    self.add( tag );
                });
            }
        },

有谁知道如何解决这个问题??提前致谢。

4

0 回答 0