调用一次js函数后页面将冻结
Page freezes after js function called once
在我的页面上,我得到了复选框。如果选中,则我们将输入字段设置为启用,否则它们将被禁用。所以当我第一次加载页面时,它工作正常。但在它刚刚冻结之后。在Chrome控制台中似乎什么都没有出现。
为什么会这样?
元素在这里
<label class="switch span2">
<input id="p_switcher" name="action" type="checkbox" class="switch-input"/>
<span class="switch-label" data-on="New Project" data-off="Open Project"></span>
<span class="switch-handle"></span>
</label>
文件看起来像
<input type="text" name="ProjectName" placeholder="Project Name" id="textProjectName" disabled/>
这是JS
<script type="text/javascript">
$(function () {
// disable not usable fileds
var handlers = function () {
$('input#p_switcher').change(function () {
//setting the atributes
var setAttr = function (fieldName, value) {
$('[name=' + fieldName + ']').attr('disabled', value);
w2ui.form.fields.filter(function (x) { return x.name == fieldName; })[0].required = !value;
}
if ($('input#p_switcher').prop("checked", true)) {
// setAttr('projectId', true);
setAttr('ProjectName', false);
setAttr('CompanyName', false);
setAttr('FieldOperator', false);
setAttr('FieldName', false);
setAttr('Unit', false);
} else {
// setAttr('projectId', false);
setAttr('ProjectName', true);
setAttr('CompanyName', true);
setAttr('FieldOperator', true);
setAttr('FieldName', true);
setAttr('Unit', true);
}
w2ui.form.refresh();
handlers();
});
}
setTimeout(handlers, 500);
});
</script>
if ($('input#p_switcher').prop("checked", true)) {
在这一行上,您实质上是在再次触发.change
事件。
我想你的意思是:
if($('input#p_switcher').prop("checked")) {
....
}
我想就是这样了…您正在从处理程序中调用处理程序,没有超时…
$(function () {
// disable not usable fileds
/*
* shortened
*/
//recursion, no timeout
handlers();
});
}
setTimeout(handlers, 500);
});
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 将函数的上下文应用于javascript变量
- 如何在JavaScript中将字符串转换为函数引用
- 用嵌套函数和默认函数定义函数
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- 如何冻结函数's在闭包中的变量
- Firefox在调用函数(jquery)时冻结
- 当调用JS函数具有alert()时,应用程序将被冻结
- JavaScript函数冻结我的浏览器
- 如何在 JavaScript 中冻结构造函数中的属性
- 如何在“后台”/不冻结UI的情况下运行javascript函数
- 停止Javascript函数,如果它冻结到长
- 我可以在 javascript 中调用在后台运行或异步使浏览器不冻结的 JavaScript 函数吗?
- 是什么导致函数“;冻结”;在javascript中
- 函数调用自己setInterval(浏览器冻结问题)
- 在Node.js脚本中调用alert()函数会冻结整个服务器吗?
- setTimeout函数完全冻结任何输出
- 调用一次js函数后页面将冻结