Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.com/on/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...