0

我得到了这段代码,用于渲染和使用 Flipswitch 作为灯光开关应用程序中的自定义控件。

function createBooleanSwitch(element, contentItem, trueText, falseText, optionalWidth) {
var $defaultWidth = '5.4em';
var $defaultFalseText = 'False';
var $defaultTrueText = 'False';
var $selectElement = $('<select data-role="slider"></select>').appendTo($(element));

if (falseText != null) {
    $('<option value="false">' + falseText + '</option>').appendTo($selectElement);
}
else {
    $('<option value="false">' + $defaultFalseText + '</option>').appendTo($selectElement);
}

if (trueText != null) {
    $('<option value="true">' + trueText + '</option>').appendTo($selectElement);
}
else {
    $('<option value="true">' + $defaultTrueText + '</option>').appendTo($selectElement);
}


// Now, after jQueryMobile has had a chance to process the 
//     new DOM addition, perform our own post-processing:
$(element).one('slideinit', function () {
    var $flipSwitch = $('select', $(element));

    // Set the initial value (using helper function below):
    setFlipSwitchValue(contentItem.value);

    // If the content item changes (perhaps due to another control being
    //     bound to the same content item, or if a change occurs programmatically), 
    //     update the visual representation of the control:
    contentItem.dataBind('value', setFlipSwitchValue);

    // Conversely, whenver the user adjusts the flip-switch visually,
    //     update the underlying content item:
    $flipSwitch.change(function () {
        contentItem.value = ($flipSwitch.val() === 'true');
    });

    // To set the width of the slider to something different than the default,
    //    need to adjust the *generated* div that gets created right next to
    //    the original select element.  DOM Explorer (F12 tools) is a big help here.
    if (optionalWidth != null) {
        $('.ui-slider-switch', $(element)).css('width', optionalWidth);
    }
    else {
        $('.ui-slider-switch', $(element)).css('width', defaultWidth);
    }

    //===============================================================//

    // Helper function to set the value of the flip-switch
    //     (used both during initialization, and for data-binding)
    function setFlipSwitchValue(value) {
        $flipSwitch.val((value) ? 'true' : 'false');

        // Having updated the DOM value, refresh the visual representation as well
        //  (required for a slider control, as per jQueryMobile's documentation)
        $flipSwitch.slider(); // Initializes the slider

        $flipSwitch.slider('refresh');

        // Because the flip switch has no concept of a "null" value
        //    (or anything other than true/false), ensure that the 
        //    contentItem's value is in sync with the visual representation
        contentItem.value = ($flipSwitch.val() === 'true');
    }
});

}

这段代码工作正常。它在屏幕上呈现翻转开关。我在弹出的编辑屏幕中显示数据。当我打开包含翻转开关的弹出窗口并且不更改 UI 上的任何数据时出现问题,我只是尝试关闭该弹出屏幕。IE 挂起,并给出错误提示正在运行长脚本。当我调试 createBoolenaSwitch 函数时,我知道它在名为 setFlipSwitchValue(value){} 的函数内进入无限循环

为什么这个函数被调用并且这是一个无限循环?

4

0 回答 0