Jquery将变量传递给对象问题

时间:2011-07-20 06:18:51

标签: jquery drupal object ahah

以下是我的代码片段:

  $('.content .form-submit').each(function(index) {
        var id = $(this).attr('id');
        var selector = "#" + id;
  }

上述变量很好。他们返回ajax-comments-submit-398#ajax-comments-submit-398(或任何形式的编号恰好在页面上)就好了。问题是我需要动态地将结果传递给下面的对象(代替下面的[ID]和[SELECTOR]。

var obj3 = { 
  "ahah": {  
    [ID]: { 
      "url": "/ajax_comments/js", 
      "event": "click", 
      "keypress": null, 
      "wrapper": "comment-form-content", 
      "selector": [SELECTOR], 
      "effect": "ajaxCommentsSubmit", 
      "method": "before", 
      "progress": { 
        "type": "1bar", 
        "message": "Please wait..." 
      }, 
      "button": { 
        "op": "Save" 
      } 
    } 
  } 
}

我试过了

var obj3 = { 
  "ahah": {  
    id: { 
      "url": "/ajax_comments/js", 
      "event": "click", 
      "keypress": null, 
      "wrapper": "comment-form-content", 
      "selector": selector, 
      "effect": "ajaxCommentsSubmit", 
      "method": "before", 
      "progress": { 
        "type": "1bar", 
        "message": "Please wait..." 
      }, 
      "button": { 
        "op": "Save" 
      } 
    } 
  } 
}

但是对象字面意思是“selector”和“id”,而不是我想传递的变量(ajax-comments-submit-398)

我将如何做到这一点?

1 个答案:

答案 0 :(得分:0)

你必须这样做:

var obj3 = { 
  "ahah": {} 
}

obj3.ahah[id] = { 
  "url": "/ajax_comments/js", 
  "event": "click", 
  "keypress": null, 
  "wrapper": "comment-form-content", 
  "selector": selector, 
  "effect": "ajaxCommentsSubmit", 
  "method": "before", 
  "progress": { 
    "type": "1bar", 
    "message": "Please wait..." 
  }, 
  "button": { 
    "op": "Save" 
  } 
}