Javascript 自动完成未正确加载-必须重新加载页面才能使其正常工作

Javascript 自动完成未正确加载-必须重新加载页面才能使其正常工作,javascript,jquery,ajax,autocomplete,Javascript,Jquery,Ajax,Autocomplete,我对这段代码有问题。当我最初加载页面时,代码不起作用。但是,如果我在我的站点上单击另一个页面,然后再次单击调用此代码的页面,它将非常有效。我尝试过删除jQueryUI脚本,但没有成功。问题在于以$event\u name开头并调用autocomplete jquery例程的代码。如果我将代码注释掉,页面将正确加载。如果我把代码放回去,我必须在加载这个页面之前先加载另一个页面 欢迎所有建议。谢谢格兰特 <script type="text/javascript"> $(do

我对这段代码有问题。当我最初加载页面时,代码不起作用。但是,如果我在我的站点上单击另一个页面,然后再次单击调用此代码的页面,它将非常有效。我尝试过删除jQueryUI脚本,但没有成功。问题在于以$event\u name开头并调用autocomplete jquery例程的代码。如果我将代码注释掉,页面将正确加载。如果我把代码放回去,我必须在加载这个页面之前先加载另一个页面

欢迎所有建议。谢谢格兰特

  <script type="text/javascript">
    $(document).ready(function(){
    $('#artist_create_event_desc').jqEasyCounter({
        'maxChars': 500,
        'maxCharsWarning': 1,
        'msgFontSize': '12px',
        'msgFontColor': '#000',
        'msgFontFamily': 'Verdana',
        'msgTextAlign': 'left',
        'msgWarningColor': '#F00',
        'msgAppendMethod': 'insertAfter'                
    });
});
    $event_name = $('#name');
    $event_address_container = $('#address-container');
    $event_id = $('#id');
    $event_name.autocomplete({
        minLength: 3,
        source: '../search_event.php',
        focus: function( event, ui ) {
            $event_name.val( ui.item.event_name );
            return false;
        },
        select: function( event, ui ) {
            $event_name.val( ui.item.event_name );
            $event_id.val( ui.item.event_id );
            $event_address_container.html( ui.item.event_desc );            

            return false;
        }
    })
    .data( "autocomplete" )._renderItem = function( ul, item ) {
        return $( "<li></li>" )
            .data( "item.autocomplete", item )
            .append( "<a>" + item.event_desc + "</a>" )
            .appendTo( ul );
    };

$('#searchme').on('click',function(e){
    e.preventDefault();
    jQuery('#city').val('');
    jQuery('#state').val('');
    jQuery('#location').val('');
    jQuery('#hiddenstate').val('');
    $('#aloader').show();
    var varzip = $('#zipcode').val();
    if (varzip == '') {
        $('#aloader').hide();
        alert('Please enter zipcode');
        return;
    }
    varzipReplaced = varzip.split(' ').join('+');
    $.ajax({
        url : 'location.php',
        data : 'zip='+encodeURIComponent(varzipReplaced),
        type : 'post',
        success : function(res) {
            $('#aloader').hide();
            var dataObj = $.parseJSON(res);
            if (dataObj.city == '' && dataObj.state == '' && dataObj.country == '') {
                $('#city').removeAttr("readonly");
                $('#state').removeAttr("readonly");
                $('#location').removeAttr("readonly");
                alert('No location found! Enter information manually');
                return;
            } else {
                $('.showZipErr').empty().append('<strong>*Zipcode*:</strong>');
                if (dataObj.city != '') {
                    $('.showCityErr').empty().append('<strong>*City*:</strong>');
                    $('#city').val(dataObj.city);
                } else {
                    $('#city').removeAttr("readonly");
                }
                if (dataObj.state != '') {
                    $('.showStateErr').empty().append('<strong>*State or Province*:</strong>');
                    $('#state').val(dataObj.state);
                } else {
                    $('#state').removeAttr("readonly");
                }

                var stateWithShortName = dataObj.stateshort + "-" +dataObj.state
                if (dataObj.state != '') {
                    $('#hiddenstate').val(stateWithShortName);
                } else {
                    $('#state').removeAttr("readonly");
                }

                if (dataObj.country != '') {
                    $('.showCountryErr').empty().append('<strong>*Country Code*:</strong>');
                    $('#location').val(dataObj.country);
                } else {
                    $('#location').removeAttr("readonly");
                }
            }
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert('Error fetching location');
        }
    });

    });
</script>

$(文档).ready(函数(){
$(“#艺术家_创建_事件_描述”).jqEasyCounter({
“maxChars”:500,
“maxCharsWarning”:1,
'msgFontSize':'12px',
“msgFontColor”:“000”,
“msgFontFamily”:“Verdana”,
'msgTextAlign':'left',
“msgWarningColor”:“F00”,
“msgAppendMethod”:“insertAfter”
});
});
$event_name=$('#name');
$event_address_container=$(“#address container”);
$event_id=$(“#id”);
$event\u name.autocomplete({
最小长度:3,
来源:“../search_event.php”,
焦点:功能(事件、用户界面){
$event_name.val(ui.item.event_name);
返回false;
},
选择:功能(事件、用户界面){
$event_name.val(ui.item.event_name);
$event_id.val(ui.item.event_id);
$event\u address\u container.html(ui.item.event\u desc);
返回false;
}
})
.数据(“自动完成”)。\u renderItem=功能(ul,项目){
返回$(“
  • ”) .data(“item.autocomplete”,item) .append(“+item.event\u desc+”) .附录(ul); }; $('searchme')。在('click',函数(e)上{ e、 预防默认值(); jQuery(“#city”).val(“”); jQuery(“#state”).val(“”); jQuery(“#location”).val(“”); jQuery('#hiddenstate').val(''); $('aloader').show(); var varzip=$('#zipcode').val(); if(varzip==''){ $('aloader').hide(); 警报(“请输入zipcode”); 返回; } varzipReplaced=varzip.split('').join('+'); $.ajax({ url:'location.php', 数据:“zip=”+encodeURIComponent(varzipReplaced), 键入:“post”, 成功:功能(res){ $('aloader').hide(); var dataObj=$.parseJSON(res); 如果(dataObj.city=''&&dataObj.state=''&&dataObj.country=''){ $(“#城市”).removeAttr(“只读”); $(“#state”).removeAttr(“只读”); $(“#位置”).removeAttr(“只读”); 警报(“找不到位置!手动输入信息”); 返回; }否则{ $('.showZipErr').empty(); 如果(数据对象城市!=''){ $('.showcityer').empty().append('*City*:); $(#city').val(dataObj.city); }否则{ $(“#城市”).removeAttr(“只读”); } 如果(dataObj.state!=''){ $('.showStateErr').empty().append('*州或省*:'); $('#state').val(dataObj.state); }否则{ $(“#state”).removeAttr(“只读”); } var stateWithShortName=dataObj.stateshort+“-”+dataObj.state 如果(dataObj.state!=''){ $('hiddenstate').val(stateWithShortName); }否则{ $(“#state”).removeAttr(“只读”); } 如果(dataObj.country!=''){ $('.showconryerr').empty().append('*国家代码*:'); $(#location').val(dataObj.country); }否则{ $(“#位置”).removeAttr(“只读”); } } }, 错误:函数(xhr、ajaxOptions、thrownError){ 警报(“获取位置错误”); } }); });
    如果可能,可以发布
    html
    ?ThanksI试图通过Stackoverflow发布代码,超过5000个字符。只是表格而已。大多数html只是Div和一个表单。可以尝试包含
    #name
    的部分吗?下面是引用#name场馆名称:场馆名称来创建活动说明的代码,而不是尝试
    jsEasyCounter
    ,可以包括
    搜索事件.php
    jsiddle中的预期返回对象/值