jQuery trigger(#39;click#39;) not working with 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:jQuery 触发器('click')不适用于 Jasmine-jquery


基础教程推荐
- 我什么时候应该在导入时使用方括号 2022-01-01
- 动态更新多个选择框 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 在for循环中使用setTimeout 2022-01-01