0

只要我在搜索 div 所在的同一页面上,此功能就可以正常工作imagesearch。如果我在网站上的另一个网站上,我会得到错误

无法读取未定义的属性“长度”。

控制台点放在第 27 行,这是 if 语句开始的地方if(q.length.

无法真正找出问题所在。有任何想法吗 ?

function reloadSearch() {
       if (!isLoading) {
          var q = $('#imagesearch').val();
          if (q.length >= 2) {
             isLoading = true;
             $.ajax({
                type: 'GET',
                url: 'core/flickr.php',
                data: 'search=' + q,
                dataType: 'html',
                beforeSend: function () {
                   $('#imageresult').html(
                      '<img src="img/loading45.gif" alt="loading..." />');
                   if (!q[0]) {
                      $('#imageresult').html(
                         '');
                      return false;
                   }
                },
                success: function (response) {
                   $('#imageresult').html(
                      response);
                }
             });
             // enforce the delay
             setTimeout(function () {
                isLoading = false;
                if (isDirty) {
                   isDirty = false;
                   reloadSearch();
                }
             }, delay);
          }
       }
    };

    var delay = 1000;
    var isLoading = false;
    var isDirty = false;

        $(document).ready(function () {
           reloadSearch();
           $('#imagesearch').keyup(function () {
              isDirty = true;
              reloadSearch();
           });
        });
4

1 回答 1

1

改变:

if (q.length >= 2) {

if (q !== undefined && q.length >= 2) {

这将停止无法从未定义中读取值...如果您在 .Net 中编码,这类似于空引用异常。

于 2013-06-12T04:52:16.500 回答