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