DOJO:如何更改已删除的节点和后续节点

时间:2012-01-17 17:28:32

标签: javascript html dojo

我有以下HTML:

   <form action="/Config.aspx/SDKHome/SaveSections" id="SaveSections" method="post">
                <input name="[0].SchemaName" type="hidden" value="contactSection" />

            <ul creator="attrCreator" data-dojo-type="dojo.dnd.Source" class="section container dojoDndSource"
                style="list-style-type: none;">

                <li class="dojoDndItem">
                            <input name="[0].DefinedAttributes[0].SchemaName" type="hidden" value="firstname" />
                </li>

                <li class="dojoDndItem">
                            <input name="[0].DefinedAttributes[1].SchemaName" type="hidden" value="jobtitle" />
                </li>
            </ul>
   </form>

我想将此项添加到列表中:

            <li class="dojoDndItem">
                        <input name="[X].DefinedAttributes[Y].SchemaName" type="hidden" value="itemToAdd" />
            </li>

如何替换X和Y(并通过以下所有项增加此值),以获得以下html:

   <form action="/Config.aspx/SDKHome/SaveSections" id="SaveSections" method="post">
                <input name="[0].SchemaName" type="hidden" value="contactSection" />

            <ul creator="attrCreator" data-dojo-type="dojo.dnd.Source" class="section container dojoDndSource"
                style="list-style-type: none;">

                <li class="dojoDndItem">
                            <input name="[0].DefinedAttributes[0].SchemaName" type="hidden" value="firstname" />
                </li>

            <li class="dojoDndItem">
                        <input name="[0].DefinedAttributes[1].SchemaName" type="hidden" value="itemToAdd" />
            </li>


                <li class="dojoDndItem">
                            <input name="[0].DefinedAttributes[2].SchemaName" type="hidden" value="jobtitle" />
                </li>
            </ul>
   </form>

1 个答案:

答案 0 :(得分:1)

试试这个:

dojo.require("dojo.dnd.Source");   
dojo.ready(function(){
    var ul = dojo.query("ul.section").pop();
    var lastLiInput = dojo.query("ul li:last-child>input").pop();
    var lastLiInputName = dojo.attr(lastLiInput, "name");   
    var X = 1 + parseInt(lastLiInputName.replace(/.*DefinedAttributes\[(\d+)\].*/, '$1'));
    var newLi = dojo.create("li", { class : "dojoDndItem" }, ul);
    dojo.create("input", {
        name : "[0].DefinedAttributes[" + X + "].SchemaName",
        type : "hidden",
        value : "otherValue"
    }, newLi); 
});

我不确定我使用的选择器是否适用于所有浏览器,但是您可以使用dom选择器来获取每个X和Y,增量并放置在正确位置的正则表达式。只需适应你的dnd逻辑......

在此处查看演示,使用可见输入而不是隐藏:http://jsfiddle.net/psoares/FSCdW