JQuery UI 1.8.23复制粘贴问题

时间:2013-02-19 20:17:00

标签: jquery jquery-autocomplete

我们正在使用以下JQuery代码(JSF)来调用自动完成功能。当用户键入数据但用户复制和粘贴时,自动完成仅在第二次尝试时才能正常工作。我正在测试IE 8中的以下代码。感谢任何输入

<script type="text/javascript">
$(function () { 
    $(document).on('keyup.autocomplete','##{resp.workItemResponse.wiResponseId}', function() {
        $('##{resp.workItemResponse.wiResponseId}').autocomplete({
            minLength: 3,
            source: function( request, response){
                $.ajax({
                    type: 'GET',
                    url: "/iaportal/autoCompleteServlet",
                    cache: false,
                    delay: 200,
                    data: {
                        'respId':'#{resp.workItemResponse.wiResponseId}',
                        'type': '#{autoCompleteType}',
                        'term':request.term
                    },
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(data){
                        response($.map(data.acctNum, function (item) {
                            return {                                
                                label: item.label,
                                value: item.label
                            }
                        }));
                    },
                    error: function(message){
                        alert("error "+message);
                    }
                });
            },
            select: function( event, ui ) {
                var selectedObj = ui.item;
                var text = selectedObj.value;
                $('.autoComplete#{resp.workItemResponse.wiResponseId}').attr('value', text);
                $('.autoComplete#{resp.workItemResponse.wiResponseId}').trigger('change'); 
            }
        });
        $('##{resp.workItemResponse.wiResponseId}').keyup(function(){
            var text = this.value;
            if(text == '' || text.length == 0){
                $('.autoComplete#{resp.workItemResponse.wiResponseId}').attr('value', "");
                $('.autoComplete#{resp.workItemResponse.wiResponseId}').trigger('change');
            }
        });
    }); // on Ends

    var textElem;
    $(document).on('paste','##{resp.workItemResponse.wiResponseId}',function() {
        textElem = this;
        setTimeout(invokeOnPaste , 100); 
    });  

    function invokeOnPaste(){
        var text = $(textElem).val();
        if(text.length == 14){ 
            $('.autoComplete#{resp.workItemResponse.wiResponseId}').attr('value', text);
            $('.autoComplete#{resp.workItemResponse.wiResponseId}').trigger('change'); 
        } else if(text.length != 0){
            $('##{resp.workItemResponse.wiResponseId}').trigger('search','autocomplete');
        } 
    }
});
</script>

1 个答案:

答案 0 :(得分:0)

除了IE(以及启用了配置选项的FireFox)之外,所有浏览器的Javascript限制了剪贴板访问。您需要绑定到其他内容,例如keyup以进行Ctrl+V粘贴。据我所知,以跨浏览器兼容的方式绑定到上下文菜单粘贴是不可能的。