代码之家  ›  专栏  ›  技术社区  ›  Josh Hunt bstahlhood

在可排序选项卡中将文本框的值更改为其当前顺序

  •  1
  • Josh Hunt bstahlhood  · 技术社区  · 17 年前

    编辑:我自己解决了这个问题。见 my answer below

    我用jquery建立了一个很好的可排序表,它非常好。但现在我想扩展它。

    每一个表格行都有一个文本框,我想我后面的是,每次删除一行,文本框都会更新以反映文本框的顺序。 例如,顶部的文本框的值总是“1”,第二个总是“2”,依此类推。

    我正在使用jquery和 Table Drag and Drop JQuery plugin

    代码

    Javascript:

    <script type="text/javascript">
        $(document).ready(function() {        
        $("#table-2").tableDnD({
            onDrop: function(table, row) {
                var rows = table.tBodies[0].rows;
                var debugStr = "Order: ";
                for (var i=0; i<rows.length; i++) {
                    debugStr += rows[i].id+", ";
                }
                console.log(debugStr)
                document.forms['productform'].sort1.value = debugStr;
                document.forms['productform'].sort2.value = debugStr;
                document.forms['productform'].sort3.value = debugStr;
                document.forms['productform'].sort4.value = debugStr;
            },
        });
    });
    

    HTML表:

    <form name="productform">
            <table cellspacing="0" id="table-2" name="productform"> 
                <thead>
                    <tr><td>Product</td>    <td>Order</td></tr> 
                </thead>
    
                <tbody>
                    <tr class="row1" id="Pol">  <td><a href="1/">Pol</a></td>   <td><input type="textbox" name="sort1"/></td>   </tr>
                    <tr class="row2" id="Evo">  <td><a href="2/">Evo</a></td>   <td><input type="textbox" name="sort2"/></td>   </tr>
                    <tr class="row3" id="Kal">  <td><a href="3/">Kal</a></td>   <td><input type="textbox" name="sort3"/></td>   </tr>
                    <tr class="row4" id="Lok">  <td><a href="4/">Lok</a></td>   <td><input type="textbox" name="sort4"/></td>   </tr>
                </tbody> 
            </table>
        </form>
    
    2 回复  |  直到 17 年前
        1
  •  1
  •   Josh Hunt bstahlhood    17 年前

    jquery中的hardnrg最终帮我解决了这个问题。

    它涉及到在每个输入中添加一个id=“”:

    <form name="productform">
        <table cellspacing="0" id="table-2" name="productform"> 
            <thead>
                <tr><td>Product</td>    <td>Order</td></tr> 
            </thead>
    
            <tbody>
                <tr class="row1" id="Pol">  <td><a href="1/">Pol</a></td>   <td><input id="Pol_field" type="textbox" name="sort1"/></td>    </tr>
                <tr class="row2" id="Evo">  <td><a href="2/">Evo</a></td>   <td><input id="Evo_field" type="textbox" name="sort2"/></td>    </tr>
                <tr class="row3" id="Kal">  <td><a href="3/">Kal</a></td>   <td><input id="Kal_field" type="textbox" name="sort3"/></td>    </tr>
                <tr class="row4" id="Lok">  <td><a href="4/">Lok</a></td>   <td><input id="Lok_field" type="textbox" name="sort4"/></td>    </tr>
            </tbody> 
        </table>
    </form>
    

    并将此JS添加到OnDrop事件:

    for (var i=0; i < rows.length; i++) {
        $('#' + rows[i].id + "_field").val(i+1);
    }
    

    轻松的生活!

        2
  •  0
  •   Sugendran    17 年前

    六羟甲基三聚氰胺六甲醚。。 我想你想做这样的事情:

    $("input:text", "#table-2").each( function(i){ this.value=i+1; });
    

    $().each()函数的信息如下: http://docs.jquery.com/Core/each