2012-03-01 59 views
1

我正在嘗試使用下拉列表的值作爲下一個函數的名稱。該字符串是正確的,並顯示在警報中。在代碼中明確寫入名稱也是可行的。但是在兩個函數的範圍內使用來自變量的部分根本不起作用。從函數傳遞給函數名的字符串變量不起作用

Drupal.behaviors.smart_inventory = { 
attach: function(context, settings) { 

    // this should be in the scope of both functions 
    var selecttype; 

    $('#select-voc-content_types', context).change(function() { 

      contenttype = $(this).val(); 
      secondary = $('#' + contenttype); 

      if($(secondary).length > 0) 
      { 
       // set the select list. tried an alert and the variable string is set 
       selecttype = '#select-voc-' + $(this).val(); 

       $('.group-types').show(); 
       $('#' + contenttype).show(); 
       $('#object-ajax-form').hide();      
      } 
      else 
      { 
       $('#object-node-form-message').show(); 
       $.post('smart_inventory/ajax', { "node-type": contenttype }, frmDrupal); 
      } 

    }); 

    // this does not respond as jquery does not accept the string as an element name 
    // or maybe the variable is not available here? 

    $(selecttype, context).change(function() { 

      var nodetype = $(this).val(); 
      $('#object-node-form-message').show(); 
      $.post('smart_inventory/ajax', { "node-type": nodetype }, frmDrupal); 

    }); 

    var frmDrupal = function(responseText) { 

     $('#object-ajax-form').show(); 
     $('#object-ajax-form').html(responseText); 
     $('#object-node-form-message').hide(); 
    } 
} 

};

如果發現這個工程!但是嵌套一個功能良好的做法?還是夠好? ;

Drupal.behaviors.smart_inventory = { 
attach: function(context, settings) { 

    var selecttype; 

    $('#select-voc-content_types', context).change(function (selecttype) { 

      contenttype = $(this).val(); 
      secondary = $('#' + contenttype); 

      if($(secondary).length > 0) 
      { 
       // set the select list 
       selecttype = '#select-voc-' + $(this).val(); 

       $('.group-types').show(); 
       $('#' + contenttype).show(); 
       $('#object-ajax-form').hide();      
      } 
      else 
      { 
       $('#object-node-form-message').show(); 
       $.post('smart_inventory/ajax', { "node-type": contenttype }, frmDrupal); 
      } 
      $(selecttype , context).change(function() { 
       var nodetype = $(this).val(); 
       $('#object-node-form-message').show(); 
       $.post('smart_inventory/ajax', { "node-type": nodetype }, frmDrupal); 
      }); 

});

+0

@tvanfossen,使用[ID^= 「選擇-VOC-」]使DOM的掃描,並觸發到函數的調用。 – 2012-03-01 15:13:14

+0

可能重複的http://stackoverflow.com/questions/4972303/how-do-i-get-ajax-contents-in-global-javascript-variable和其他許多 – 2012-03-01 15:13:43

+0

要非常小心你不要開始雙註冊事件。 – DefyGravity 2012-03-01 16:39:13

回答

0

您的selecttype變量在您嘗試使用時尚未設置 - 只有在觸發了ID爲select-voc-content_types的元素上的更改處理程序時,纔會設置此變量。

0

問題是該變量在處理程序連接時沒有值。處理它的最簡單方法是使用屬性「starts with」選擇器來匹配所有可能的選擇類型,而不是嘗試爲每個選擇類型應用處理程序。然後,您可以根據需要使用選定的選擇類型在處理程序中進行篩選。

$('[id^="select-voc-"]',context).change(function() { 
     if (this.id == selecttype) { 
      ... 
     } 
     else { 
      return false; // this isn't the correct select type, prevent the action 
     } 
}); 
0
//selecttype changes during an event fire. event registration happens way before then. just do this. 
    $('[id^="select-voc-"]', context).change(function() { 

      var nodetype = $(this).val(); 
      $('#object-node-form-message').show(); 
      $.post('smart_inventory/ajax', { "node-type": nodetype }, frmDrupal); 

    }); 


// depending on your version of jQuery, go for .on wherever possible: 

$(context).on({ 
'change':function(evt){ 
    var nodetype = $(this).val(); 
       $('#object-node-form-message').show(); 
       $.post('smart_inventory/ajax', { "node-type": nodetype }, frmDrupal)} 
},'[id^="select-voc-"]','');