1

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

<pre>
   <code>
      <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>
   </code>
</pre>
4

0 回答 0