jQuery触发器('click')不能与Jasmine-jquery一起使用 [英] jQuery trigger('click') not working with Jasmine-jquery

查看:135
本文介绍了jQuery触发器('click')不能与Jasmine-jquery一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的测试代码:

describe("Login", function(){
    beforeEach(function(){
        loadFixtures('login-fixture.html');
    })

    it("should enable the button when checking 'remember password'", function(){
        $('#remember').trigger('click');
        expect($('#keepIn')).not.toBeDisabled();
    });
});

这是我的生产代码:

$(document).ready(function(){

    $('#remember').click(function(e) {
        if($('#remember').is(':checked'))
        {
            $('#keepIn').removeAttr('disabled');
        }
    });

});

这不起作用,生产代码永远不会被调用。我已经在触发事件之前和之后发出警报,并且在触发之后选中了复选框,但是.click函数没有被调用。

This is not working, the production code never gets called. I have put alerts before and after the trigger event and after the trigger the checkbox is checked, but the .click function does not get called.

对于为什么这是发生了吗?

Any thoughts on why is this happening?

推荐答案

在没有看到其余代码的情况下,我假设login-fixture.html包含 #remember复选框。如果是这样,它在DOM加载后加载。这意味着您要分配的单击事件仅适用于先前加载的元素。 jQuery on()事件将为新加载的元素分配您想要的任何事件。您可能想尝试向该id添加on()事件。类似于:

Without seeing the rest of the code, I'm assuming the "login-fixture.html" contains the "#remember" checkbox. If so, it's loading after the DOM loads. Meaning that the 'click' event you want assigned will only apply to previously loaded elements. The jQuery on() event will assign any event you want to newly loaded elements. You might want to try adding a on() event to that id. Something like:

$(function(){
    $('#remember').on('click', function(){
        if($('#remember').is(':checked')){
            $('#keepIn').checkboxradio('enable');
        }
    });
});

希望有所帮助。

请参阅: http://api.jquery.com/on/

这篇关于jQuery触发器('click')不能与Jasmine-jquery一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆