代码之家  ›  专栏  ›  技术社区  ›  hoju

文本框更新事件

  •  1
  • hoju  · 技术社区  · 16 年前

    我有一个文本框,并希望一个事件触发时,它是更新,无论是通过按键,从剪贴板粘贴,或其他任何可能的。

    绑定到keyup事件就足够了还是有更好的方法?

    3 回复  |  直到 16 年前
        1
  •  1
  •   Keyne Viana    16 年前

    $(document).ready(function(){
    
        $('#link').keyup(function(event){       
    
            if($(this).val().length > 30 && this.value != this.lastValue){
                if (this.timer) clearTimeout(this.timer);
    
                if(!$('#ajax-aguarde').length)
                {
                    $('<p id="ajax-aguarde"></p>').appendTo($('#link-element'));
                }
                $('#ajax-aguarde').html('Wait, loading...');
    
                if($('#posts').length){
                    $('#posts').remove();
                    $('#p-ultimos').remove();
                }
    
                str = this.value;
                url = $('#ajaxurl').val();
    
                this.timer = setTimeout(function () {
                    $.post(
                    url,
                    { link: str, format: 'json' }, 
                    function(data){
    
                        if(data.error == 1){
                            $('#ajax-aguarde').html('Error...');
                        } else {
                            $('#ajax-aguarde').remove();
                        }
    
                        if(data.titulo.length){
                            $('#titulo').val(data.titulo);
                        }
                        if(data.descricao.length){
                            $('#descricao').val(data.descricao);
                        }
    
                        $('<p id="p-ultimos">Last posts:</p>').appendTo($('#link-element'));
                        $('<ul id="posts"></ul>').appendTo($('#link-element'));
    
                        $.each(data.posts, function(index, post) { 
                            $('<li id="post' + index + '"></li>').appendTo($('#posts'));
                            $('#post' + index).html(post.title);
                        });
    
    
                    },
                    "json"
                    );
                },1000);
    
                this.lastValue = this.value;
            }
    
        });
    
        setTimeout(function() { $('#link').keyup(); },500);
    });
    

        2
  •  2
  •   carillonator    16 年前

    可以像这样绑定多个事件:

    $('#textbox').bind('keyup change blur', function() {
        // stuff to do when any of these events fire
    });
    

    change() 不过,一个人应该对你有用。

    Here is the API doc

        3
  •  0
  •   mcgrailm    16 年前