jQuery .on触发器不触发动作

时间:2012-02-02 11:15:32

标签: jquery

我有这样的表格:

$form = array(
        '<form name="backup-form" class="backup-form" method="post" action="#">',
            '<fieldset class="backup-fields">',
                '<fieldset class="display-wrapper">',
                    '<label for="display" class="display-label">Backups</label>',
                    '<input type="text" name="display" class="display" value="' . $this->backups(false,true) . '" autocomplete="off" readonly="readonly" />',
                '</fieldset>',
                '<fieldset class="action-wrapper">',
                    '<button type="submit" name="clean" class="clean">Clean All</button>',
                    '<button type="submit" name="create" class="create">Create</button>',
                '</fieldset>',
            '</fieldset>',
        '</form>',
        '<div class="message-handling" class="message-handling"></div>'
    );
$form = implode("", $form);
echo $form;

我正试图将偶数绑定到每个提交按钮,如下所示:

$('.backup-form .clean').on({
    click: function() {console.log('Clean Button Pressed');}
});

or

$('.main-actions .backup-form').delegate('.clean', 'submit', function() {
    console.log('Clean Button Pressed');    
});

但它都不起作用。发生了什么 ?或者我该怎么做才能使它发挥作用?

1 个答案:

答案 0 :(得分:1)

尝试,如果您要动态添加表单

$(function(){
    $(document).delegate('.clean', 'submit', function() {
        console.log('Clean Button Pressed');    
    });
}

或者如果不是这样的话

$(function(){
$('.main-actions .backup-form').delegate('.clean', 'submit', function() {
    console.log('Clean Button Pressed');    
});
}