在Drupal 7中没有调用Javascript函数,而Drupal 7曾在Drupal 6中正常运行

Javascript function not getting called in Drupal 7 which used to run proper in Drupal 6

本文关键字:Drupal 曾在 正常运行 调用 Javascript 函数      更新时间:2023-09-26

我正在将Drupal 6站点迁移到Drupal 7。因此,我在代码方面遇到了一些问题。我读到的Drupal7中对带有jquery等的javascript的调用有所不同。这是我在Drupal6中看到的页面的图片。在更改下拉框时,它上面的表中会填充属于该下拉框的项目地位这在Drupal 6中运行良好。但在Drupal7中,我看到了这个javascript错误。

这是我的php文件中Dropdown的表单元素。

<? php
 $form['status_list'] = array(
'#type' => 'select',
'#title' => t('Freeway Project Statuses'),
'#options' => array(
  0 => t('-Select Status-'),
  1 => t('Draft'),
  2 => t('NotSpecified'),
  3 => t('Quote'),
  4 => t('Forecasted'),
  5 => t('InEvaluation'),
  6 => t('Cancelled'),
  7 => t('Booked'),
  8 => t('InProduction'),
  9 => t('Completed'),
  10 => t('Closed'),
 ),
 '#default_value' => array('0' => 'Select Status'),
 '#weight' => 0,
);

这是JavaScript我已经用Drupal 7 的附加标签修改了它

 (function$){ .. })(jQuery); . 

Drupal 6中不需要这些标签。

      (function$){
    $(document).ready(function () {
     $("#edit-status-list").change(function() {
     var selectedStatus = $(this).find(":selected").text();
     var charExists = ((window.location.href).indexOf('?') >= 0) ? true : false;
    if(charExists){
    var split = (window.location.href).split('?');
    var loc = split[0];
      loc = loc+"?status="+selectedStatus;  
    self.location.href= loc;
    }
    else{
     var locf = window.location.href+"?status="+selectedStatus;   
     self.location.href= locf;
     }
     });
     $("#create-freeway-project").submit(function() {
      $(":submit", this).attr("disabled", "disabled");
     });
    $(".common_link_class").click(function() {
          //alert("Hello");
       var count = ($(this).data("clicks") || 0) + 1;
       $(this).data("clicks", count);

        if ($(this).data("clicks") >= 1) {
       }

       if ($(this).data("clicks") >= 2) {
         return false;
       }
     });

      $("#edit-analysis-code-one").change(function () {
        // Get the configs and split them.
        var cfgs = _get_configs("edit-custRef");
        var split_cfgs = cfgs.split('/');
        // Create some new configs and put it back into the configs  textfield.
        //var new_cfgs = $(this).val() +"/"+ cfgs[1];
        var new_cfgs = $("#edit-analysis-code-one option:selected").text() +"/"+ cfgs[1];
        $("#edit-custRef").val(new_cfgs);
      });
      $("#edit-analysis-code-two").change(function () {
        // Get the configs and split them.
        var cfgs = _get_configs("edit-custRef");
        var split_cfgs = cfgs.split('/');
        // Create some new configs and put it back into the configs textfield.
        //var new_cfgs = cfgs[0] +"/"+ $(this).val();
        var new_cfgs = cfgs[0] +"/"+ $("#edit-analysis-code-two option:selected").text()
        $("#edit-custRef").val(new_cfgs);
     });

   });
 })(jQuery); 

(function$){
function _get_configs(id) {
  return $("#"+ id).val();
}   
})(jQuery); 

#edit_status_list是我们感兴趣的下拉列表。

我想听听您对Drupal 7 中正确的javascipt调用需要做哪些额外更改的建议

应该是

(function($) {

(function$ {

你得到的是一个语法错误。第一个实例化一个函数,使得它的第一个参数将通过其主体代码中的符号$可用。函数在正文之后调用:

  // ...
})(jQuery);

全局"jQuery"对象作为参数传入。此设置确保您的代码通过通用的"$"符号访问正确的框架(即jQuery),而不必担心"$"的另一个外部绑定。