admin管理员组

文章数量:1389754

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/

本文标签: javascriptjQuery trigger(39click39) not working with JasminejqueryStack Overflow