2

We are using following JQuery code (JSF) to invoke Autocomplete. Everthing seems to work fine when user keys in data but when user copies and pastes Autocomplete will work only in second attempt. I am testing following code in IE 8. Any inputs are appreciated

<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>
4

1 回答 1

0

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

于 2013-02-19T20:26:44.317 回答