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
基础教程推荐
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- Bokeh Div文本对齐 2022-01-01
