从函数传递给函数名的字符串变量不起作用

时间:2012-03-01 14:52:59

标签: javascript jquery drupal

我正在尝试使用下拉列表的值作为下一个函数的名称。字符串是正确的并显示在警报中。在代码中明确地写名称也可以。但是在两个函数范围内使用变量中的部分根本不起作用。

    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);
          });

});

3 个答案:

答案 0 :(得分:0)

当您尝试使用它时,selecttype变量尚未设置 - 只有在触发了标识为select-voc-content_types的元素的更改处理程序时才会设置它。

答案 1 :(得分:0)

问题是变量在附加处理程序时没有值。处理它的最简单方法是使用属性"以"开头。选择器匹配所有可能的选择类型,而不是尝试为每个选择类型应用处理程序。然后,您可以根据需要使用所选的选择类型在处理程序中进行过滤。

$('[id^="select-voc-"]',context).change( function() {
       if (this.id == selecttype) {
          ...
       }
       else {
          return false; // this isn't the correct select type, prevent the action
       }
});

答案 2 :(得分: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-"]','');