如何Diplay"没有找到结果"在使用$ .getJSON的自动填充中

时间:2016-07-28 07:59:10

标签: jquery

我想展示"没有找到结果"响应为空时的消息。在某些情况下,当后端会话到期时,它将返回登录页面,但我需要json数据,以便如何处理这个html数据。

    $( "#customers_name" )
  .bind( "keydown", function( event ) {
    if ( event.keyCode === $.ui.keyCode.TAB &&
        $( this ).autocomplete( "instance" ).menu.active ) {
      event.preventDefault();
    }
  })
  .autocomplete({
    source: function( request, response ) {
      $.getJSON( "ajax_functions.php", {
        term: extractLast( request.term ),
        console: $('select[name="console"] option:selected').val(),
        call: 'getCustomersEmailList',
        nmsAdmin: '<?php echo tep_session_id();?>'
      }, response );
    },
    change: function (event, ui) {
        if(!ui.item){
            $("#customers_name").val("");
        }
    },
    focus: function() {

      return false;
    },
    select: function( event, ui ) { 
      var customer_id = ui.item.customer_id;
      var customer_email = ui.item.customerEmail;
      console.log(ui);
      $('#customer_id').val(customer_id);
      $('#customer_email').val(customer_email);
      var terms = split( this.value );

      terms.pop();

      terms.push( ui.item.value );

      terms.push( "" );
      this.value = terms.join( "" );
      return false;
    }
});

1 个答案:

答案 0 :(得分:3)

我已经编辑了你的代码,并为我工作。你可以查看下面的代码。

$( "#customers_name" )
  .bind( "keydown", function( event ) {
    if ( event.keyCode === $.ui.keyCode.TAB &&
        $( this ).autocomplete( "instance" ).menu.active ) {
      event.preventDefault();
    }
  })
  .autocomplete({
    source: function( request, response ) {
      $.getJSON( "ajax_functions.php", {
        term: extractLast( request.term ),
        console: $('select[name="console"] option:selected').val(),
        call: 'getCustomersEmailList',
        nmsAdmin: '<?php echo tep_session_id();?>',
        dataType:'json',
      },response ).fail(function(jqXHR, status, error){ document.location.href='login.php'; });
    },
    response: function(event, ui) {
        console.log(ui.content);
        if (ui.content.length === 0) {
            alert("No results found");
        }
    },
    change: function (event, ui) {
        if(!ui.item){
            $("#customers_name").val("");
        }
    },
    focus: function() {

      return false;
    },
    select: function( event, ui ) { 
      var customer_id = ui.item.customer_id;
      var customer_email = ui.item.customerEmail;
      console.log(ui);
      $('#customer_id').val(customer_id);
      $('#customer_email').val(customer_email);
      var terms = split( this.value );

      terms.pop();

      terms.push( ui.item.value );

      terms.push( "" );
      this.value = terms.join( "" );
      return false;
    },
    error : function() { document.location.href='login.php';}
});