jQuery UI排序无法使用动态表

jQuery UI Sortable Not Working With Dynamic Table

本文关键字:动态 UI 排序 jQuery      更新时间:2023-09-26

我正在动态创建一个表,并试图使每一行(tr)都可排序。我已经阅读了jQueryUI网站上的各种文档,并认为我已经理解了。然而,我似乎不太明白。

我哪里错了?谢谢

 <!doctype HTML>
    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="style.css">
    <link rel="stylesheet" type="text/css" href="bootstrap.min.css">
    <link href="css/bootstrap-form-helpers.min.css" rel="stylesheet" media="screen">
    <link href="jquery-ui.css" type="text/css" rel="stylesheet">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script type="text/javascript>" href="jquery-ui.js"></script>
    <script type="text/javascript" href="bootstrap-2.2.2.min.js"></script>
    <script>
        $(function() {
            $( "tr" ).sortable();
            $( "tr" ).disableSelection();
        });
    </script>
    </head>
    <body>
    <div class="center">
    <form id="agenda_form">
        <p>Session Title</p>
        <input type="text" id="session_title">
        <p>Start Time<p>
        <input type="number" id="start_time">
        <p>End Time</p>
        <input type="number" id="end_time">
        <input type="submit" value="Submit" id="submit_form">
    </form>
    <table class="table-striped">
        <tr>
            <td>Session Title</td>
            <td>Start Time</td>
            <td>End Time</td>
        </tr>
    </table>
    </div>
    <script type="text/javascript">
        $("#submit_form").click(function (event) {
            event.preventDefault();
            var $tr = $('<tr />');
            $tr.append($("<td />", { text: $("#session_title").val()} ))
            $tr.append($("<td />", { text: $("#start_time").val()} ))
            $tr.append($("<td />", { text: $("#end_time").val()} ))
            $tr.appendTo("table");
            $("#agenda_form").each(function (){
                this.reset();
            });
        });
    </script>
    </body>
    </html>

您需要在jquery选择中使用tbody而不是tr。我还建议你把你的头做成一个扇形块——http://jsfiddle.net/rcottkqx/1/

<div class="center">
<form id="agenda_form">
    <p>Session Title</p>
    <input type="text" id="session_title">
    <p>Start Time
        <p>
            <input type="number" id="start_time">
            <p>End Time</p>
            <input type="number" id="end_time">
            <input type="submit" value="Submit" id="submit_form">
</form>
<table class="table-striped">
    <thead>
        <th>Session Title</th>
        <th>Start Time</th>
        <th>End Time</th>
    </thead>
</table>

$("#submit_form").click(function (event) {
    event.preventDefault();
    var $tr = $('<tr class="t" />');
    $tr.append($("<td />", {
        text: $("#session_title").val()
    }));
    $tr.append($("<td />", {
        text: $("#start_time").val()
    }));
    $tr.append($("<td />", {
        text: $("#end_time").val()
    }));
    $tr.appendTo("table");
    $("#agenda_form").each(function () {
        this.reset();
    });
    $("tbody").sortable();
    $("tbody").disableSelection();
});