Var和function的工作顺序错误
var and function working in the wrong order
我试图用下面的代码实现的是,当生成按钮被点击,如果html
变量被选中,然后做一些事情-在这种情况下加载一个警报,但是从这个fiddle你可以看到它是在完全相反的方式工作。
它为什么这样做?以及我如何解决它的工作方式我想要的-你可以看到,我也必须为另一个复选框做:)
HTML: <div class="form-group formGroup10">
<div class="checkbox">
<label><input id="html" type="checkbox" value="">
<strong>Download as a HTML file</strong></label>
<br/>
<label><input id="showInline" type="checkbox" value="">
<strong>Show as HTML Code</strong></label>
</div>
<br/>
<button type="button" id="generate" class="btn btn-primary formButton10">Generate</button>
</div>
JS:
var html = $('#showInline');
var file = $('#html');
$("#generate").click(function(){
html.change(function(){
if($(this).is(':checked'))
{
alert('hello');
}
});
});
不需要更改事件。你可以把它拆下来。
使用方法:
$("#generate").click(function(){
//html.change(function(){
if(html.is(':checked'))
{
alert('hello');
}
//});
});
你在做什么:
将change
事件绑定到click
事件。因此,当您第一次单击它时,它会在html
上注册更改事件。
对于另一个复选框我也要这样做
可以简化为:
$("#generate").click(function(){
if($(this).closest('.form-group').find('input[type="checkbox"]:checked').attr('id') === "showInline")
{
alert('showInline');
}else{
alert('html');
}
});
每次点击生成时都绑定一个更改事件。你应该改成
var html = $('#showInline');
var file = $('#html');
html.change(function(){
if($(this).is(':checked'))
{
alert('hello');
}
});
$("#generate").click(function(){
html.trigger('change')
});
相关文章:
- Javascript执行顺序错误
- 带有 CommonsChunkPlugin 的 Webpack 会导致 html 文件中的捆绑顺序错误
- ES6模块初始化顺序错误
- 在Firefox中的keydown事件中隐藏元素时焦点顺序错误
- 使用 JS 填充 HTML 表,数据顺序错误
- JavaScript 排序顺序错误
- ReactJS:Dom 渲染顺序错误
- kafka节点的消息顺序错误
- 角度指令后链接顺序错误(1.3)
- 循环运行顺序错误的Javascript
- AJAX调用顺序错误(异步)javascript
- node.js(谷歌日历api,mongojs)中循环的顺序错误
- Angular2-NgFor内部树模型:删除然后添加元素时顺序错误
- 数组填充顺序错误
- ASP.. Net MVC jQuery和插件加载顺序错误
- 在wordpress子主题中排队脚本顺序错误
- Var和function的工作顺序错误
- 使用LABjs的执行顺序错误
- 函数执行顺序错误
- Cookie和模态发送报头顺序错误