jQuery 触发器('click')不适用于 Jasmine-jquery

jQuery trigger(#39;click#39;) not working with Jasmine-jquery(jQuery 触发器(click)不适用于 Jasmine-jquery)
本文介绍了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() 事件会将您想要的任何事件分配给新加载的元素.您可能想尝试将 on() 事件添加到该 ID.比如:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)