jquery:动态绑定事件以形成元素

时间:2010-12-22 06:33:12

标签: jquery search constructor

我正在尝试构建一个基于jquery的高级搜索查询构造函数。使用MooTools框架构建的设计的实时版本在http://opl.bibliocommons.com/search

现在问题是,我有一个带有控件的div,必须在点击“添加更多”按钮时动态创建。创建新div后,我需要将事件绑定到div内的控件。

Mootools版本的代码如下:

add_query_part: function(ev) {
    if(ev) ev.stop();
    this.query_chunks[0].addClass('removable');
    var query_chunk = this.query_chunks[0].clone().set({'class': 'query_chunk query_piece','id':"query_chunk_"+(++this.term_count)}).inject($('query_parts'));
    var search_param_select = query_chunk.getElement('select').set({
      'class': 'parameter',
      'id':'parameter_'+this.term_count,
      'style': 'margin-right:3px'
    });
    var keyword = query_chunk.getElement('input[type=text]').set({
      'class': 'operand text',
      'id':'keyword_'+(this.term_count),
      'style':'margin-right:3px',
      'value': ''
    });
    var remove_btn = query_chunk.getElement('a').set({
      'class': 'remove_btn',
      'id':'remove_'+(this.term_count)
    });
    $("query_chunk_"+this.term_count).addClass('removable');

    this.query_chunks.push(query_chunk);
    remove_btn.addEvent('click', this.remove_query_part.bindWithEvent(this));
    keyword.addEvent('keyup', this.construct_query.bindWithEvent(this));
    keyword.addEvent('mouseup', this.construct_query.bindWithEvent(this));
    search_param_select.addEvent('change', this.construct_query.bindWithEvent(this));
    return query_chunk;
  },

1 个答案:

答案 0 :(得分:0)

jQuery live()会帮助你做你想做的事情!

相关问题