调用一次js函数后页面将冻结

Page freezes after js function called once

本文关键字:冻结 函数 js 一次 调用      更新时间:2023-09-26

在我的页面上,我得到了复选框。如果选中,则我们将输入字段设置为启用,否则它们将被禁用。所以当我第一次加载页面时,它工作正常。但在它刚刚冻结之后。在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);   
   });