javascript - jQuery trigger('click') not working with Jasmine-jquery - Stack Overflow

This is my test code:describe("Login", function(){beforeEach(function(){loadFixtures('lo

This is my test code:

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

And this is my production code:

$(document).ready(function(){

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

});

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?

This is my test code:

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

And this is my production code:

$(document).ready(function(){

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

});

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?

Share Improve this question edited Jun 25, 2012 at 15:17 mornaner asked Jun 25, 2012 at 15:09 mornanermornaner 2,4242 gold badges28 silver badges39 bronze badges 4
  • Where does that ".checkboxradio" thing e from? – Pointy Commented Jun 25, 2012 at 15:18
  • what errors are thrown in console? – charlietfl Commented Jun 25, 2012 at 15:20
  • @Pointy It's jQuery-mobile. Anyways i've changed the code to do it just with jQuery – mornaner Commented Jun 25, 2012 at 15:21
  • @charlietfl No errors in console just the test doesn't pass. I've seen that doesn't get called debugging it with firebug – mornaner Commented Jun 25, 2012 at 15:23
Add a ment  | 

1 Answer 1

Reset to default 6

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

Hope that helps.

See: http://api.jquery./on/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744691172a4588217.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信