2015-03-13 40 views
0

编辑:我猜这部分是我没有经验的Drupal问题。我在site.info中添加了一个JavaScript文件,以便将其添加到每个页面。这是所有的文件包括:jQuery onclick函数:未定义不是函数

(function ($){ 
$("#ctl00_btnSearch001").on("click", function(){ 
     var searchVal = $("#ctl00_txtSearch").val(); 
     window.location.href = "http://www.mywebsite.org/search/?sa=Search&q=" + searchVal;   
    });  
})(jQuery); 

当网站加载时,它被编译成这个更大的脚本,它看起来像这样在调试器:

(function ($) { 
    Drupal.behaviors.titlebar = { 
    init: function(context, settings) { 
     // Using percentage font size to easily increase/decrease page font size 
     var baseFontSize = 100; 
     $('.pgc-font-size a').click(function() { 
     if($(this).hasClass('increase')) { 
      if(baseFontSize < 150) 
      baseFontSize += 20; 
      $('.pg-content-body p').css('font-size', baseFontSize+'%'); 
     } else { 
      if(baseFontSize > 70) 
      baseFontSize -= 10; 
      $('.pg-content-body p').css('font-size', baseFontSize+'%'); 
     } 
     }); 

     // Print button 
     $('.pgc-print a').click(function() { 
     window.print(); 
     }) 
    } 
    }; 
}(jQuery)); 
// There's a problem with our jQuery loading before the ingested site's 
// jQuery which is causing jQuery plugins to break (the "once" plugin in this case). 
// I'm using this workaround for now 
jQuery(function() { 
    Drupal.behaviors.titlebar.init(); 
});; 
(function ($) { 
    Drupal.behaviors.giftTypes = { 
    init: function() { 
     // Gift details accordion 
     $('.pg-gift-details .accordion-items').css('display', 'none'); 
     $('.pg-gift-details .accordion-switch').click(function(){ 
     if($(this).hasClass('open')) { 
      $(this).find('span').removeClass('icon-arrow-up').addClass('icon-arrow-down'); 
      $('.pg-gift-details .accordion-items').slideUp('slow'); 
      $(this).html($(this).html().replace('Hide', 'Show More')); 
      $(this).removeClass('open'); 
     } else { 
      $(this).find('span').removeClass('icon-arrow-down').addClass('icon-arrow-up'); 
      $('.pg-gift-details .accordion-items').slideDown('slow'); 
      $(this).html($(this).html().replace('Show More', 'Hide')); 
      $(this).addClass('open'); 
     } 
     }) 
    } 
    } 
}(jQuery)); 
// There's a problem with our jQuery loading before the ingested site's 
// jQuery which is causing jQuery plugins to break (the "once" plugin in this case). 
// I'm using this workaround for now 
jQuery(function() { 
    Drupal.behaviors.giftTypes.init(); 
});; 
(function ($){ 
$("#ctl00_btnSearch001").on("click", function(){ 
    var searchVal = $("#ctl00_txtSearch").val(); 
    alert(searchVal); 
    window.location.href = "http://www.mywebsite.org/search/?sa=Search&q=" + searchVal; 
    }); 
})(jQuery); 
; 

你可以在看我的小脚本在那里。它说第一行有什么问题,但我不确定问题是什么。我需要对我的JavaScript文件进行哪些更改以确保其编译正确?


我可能忽略了一个非常简单的类型,但我看不出我的jQuery有什么问题。

这是不工作的一部分:我有jQuery的我的网站上

(function ($){ 
    $("#ctl00_btnSearch001").on("click", function(){ 
     var searchVal = $("#ctl00_txtSearch").val(); 
     window.location.href = "http://www.website.org/search/?sa=Search&q=" + searchVal; 
    }); 
})(jQuery); 

,我知道我这样做,因为这样它前面的代码没有问题使用。该错误显示在第一行的调试器,“$(‘#ct100_btnSearch001’)上(‘点击’,函数(){”这里是脚本页的大段:

(function($) { 
    Drupal.behaviors.giftTypes = { 
     init: function() { 
      // Gift details accordion 
      $('.pg-gift-details .accordion-items').css('display', 'none'); 
      $('.pg-gift-details .accordion-switch').click(function() { 
       if ($(this).hasClass('open')) { 
        $(this).find('span').removeClass('icon-arrow-up').addClass('icon-arrow-down'); 
        $('.pg-gift-details .accordion-items').slideUp('slow'); 
        $(this).html($(this).html().replace('Hide', 'Show More')); 
        $(this).removeClass('open'); 
       } else { 
        $(this).find('span').removeClass('icon-arrow-down').addClass('icon-arrow-up'); 
        $('.pg-gift-details .accordion-items').slideDown('slow'); 
        $(this).html($(this).html().replace('Show More', 'Hide')); 
        $(this).addClass('open'); 
       } 
      }) 
     } 
    } 
}(jQuery)); 

jQuery(function() { 
    Drupal.behaviors.giftTypes.init(); 
});; 
(function($) { 
    $("#ctl00_btnSearch001").on("click", function() { 
     var searchVal = $("#ctl00_txtSearch").val(); 
     alert(searchVal); 
     window.location.href = "http://www.mywebsite.org/search/?sa=Search&q=" + searchVal; 
    }); 
})(jQuery);; 
+0

用'.val();'行是否会给出错误? – A1rPun 2015-03-13 15:19:59

+1

你为什么要访问document.ready之外的DOM? – JLRishe 2015-03-13 15:21:06

+0

你使用的是什么版本的Drupal? – 2015-03-13 15:21:09

回答

0

尝试安装jQuery update模块。

如果您正在使用的Drupal 6,你是不是能够使用on功能。

一个选项是在您的page.tpl.php中的jQuery的自定义版本,另一个选项(不推荐)是使用live,但现在已弃用

0

可以绑定到一个事件的函数使用两种方式:

1.使用bind()方法和事件名称作为第一个参数

$("#foo").bind("click", function() { 
alert("User clicked on 'foo.'"); 
}); 

2.just使用事件方法

$("#foo").click(function() { 
alert("User clicked on 'foo.'"); 
}); 

您的代码的问题是,没有on事件。 ref http://api.jquery.com/category/events/mouse-events/

+0

我改变它为$(“#ct100_btnSearch001”)。click(function(){但它仍然不会我认为它认为jquery是未定义的,但它在页面上的其他地方使用,所以这似乎很奇怪我 – 2015-03-13 15:32:22

+0

你真的有一个ID为'ct100_btnSearch001'的对象吗? – 2015-03-13 15:34:07

+0

是的,我真的 – 2015-03-13 15:39:08

0

如果ctl00_btnSearch001是您尝试点击的正确标识。尝试将其更改为:

(function ($){ 
    $(document).on("click", "#ctl00_btnSearch001", function(){ 
     var searchVal = $("#ctl00_txtSearch").val(); 
     window.location.href = "http://www.mywebsite.org/search/?sa=Search&q=" + searchVal;   
    });  
})(jQuery);