尝试使用 id==valores0 注册小部件,但该 id 已注册

2022-01-24 00:00:00 loops slider javascript dojo

我得到这个错误,我不知道如何解决.我阅读了 此链接 之前.

i get this error, and i don't know how can be solved. I read this link before.

1

index.php

<script type="text/javascript">
$(document).ready(function() {   
    $("#customForm").submit(function() {
        var formdata = $("#customForm").serializeArray();

        $.ajax({
            url: "sent.php",
            type: "post",
            dataType: "json",
            data: formdata,
            success: function(data) {
                switch (data.livre) {
                case 'tags':
                    $("#msgbox2").fadeTo(200, 0.1, function() {
                        $(this).html('Empty tags').fadeTo(900, 1);
                    });
                    break;

                default:
                    $("#msgbox2").fadeTo(200, 0.1, function() {
                        $(this).html('Update').fadeTo(900, 1, function() {
                            $('#conteudo').load('dojo/test_Slider.php');   
                        });
                    });
                    break;
                }
            }
        });

        return false;
    });
});
</script>

test_slider.php

test_slider.php

<script type="text/javascript">

var slider = [];

for (i = 0; i < 5; i++) {

    slider[i] = (

    function(i) {

        return function() {

            var node = dojo.byId("input"+[i]);
            var n = dojo.byId("valores"+[i]);

            var rulesNode = document.createElement('div'+[i]);
            node.appendChild(rulesNode);

            var sliderRules = new dijit.form.HorizontalRule({
                count:11,
                style:{height:"4px"}
            },rulesNode);

            var labels = new dijit.form.HorizontalRuleLabels({
                style:{height:"1em",fontSize:"75%"},
            },n);

            var theSlider = new dijit.form.HorizontalSlider({
                value:5,
                onChange: function(){
                    console.log(arguments);
                },
                name:"input"+[i],
                onChange:function(val){ dojo.byId('value'+[i]).value = dojo.number.format(1/val,{places:4})},
                style:{height:"165px"},
                minimum:1,
                maximum:9,
                   }
            },node);

            theSlider.startup();
                sliderRules.startup();
        }

    })(i);
    dojo.addOnLoad(slider[i]);
}

</script>

问题:首先点击提交 btn 一切正常,导入了 5 个滑块.第二次单击,应该更新,但我收到此消息:

Problem: First click in submit btn all works well, 5 sliders are imported. Second click, an update is supposed, but i get this message:

Tried to register widget with id==valores0 but that id is already registered

[演示视频]2

推荐答案

只是为了补充@missingo 的答案和@Kevin 的评论.您可以通过查看注册表来浏览现有的 dijit:

Just to add on to @missingo's answer and @Kevin's comment. You could walk through the existing dijits by looking in the registry:

var i = i || 0; // Cache this at the end of your loop
dijit.registry.map(function (widget) {
    if (+widget.id.replace(/^[^d]+/, '') <  i) {
        widget.destroyRecursive();
    }
});
/*
    Your loop fixed as described in missingno's answer.
*/

相关文章