引导添加行的表(Bootstrap adding rows to a table)

2019-10-20 10:26发布

我试图动态行添加到我的餐桌。 出于某种原因,当我添加行的表,它有所有揉成一团,并在整个行不跨越。 为什么会发生这种情况,如何解决呢?

这里是我的文件,如果你想运行它,明白我的意思

jQuery的:

$(document).ready(function () {

    $('.close').on('click', function (e) {
        e.preventDefault();
        $(this).parent().parent().remove();
    });

    $('#submit').click(function () {
        var name = $('input[name=name]').val();
        var phone = $('input[name=phone]').val();
        var email = $('input[name=email]').val();


        var tr = "<tr>\
                    <td>\
                        <a href=\"#" + name + "openModal\">" + name + " </a>\
                            \
                            <div id=\"" + name + "openModal\" class=\"modalDialog\">\
                                <div>\
                                    <a href=\"#close\" title=\"Close\" class=\"close\">X</a>\
                                    <h2> " + name + "</h2>\
                                        ><p>Phone: " + phone + "</p>\
                                        <p>email: " + email + "</p>\
                                </div>\
                            </div>\
                    </td>\
                    <td> \
                        <input type='radio' name=\"" + name + "present\">In<br>\
                        <input type='radio' name=\"" + name + "present\">Out\
                    </td>\
                    <td>\
                        <button type=\"button\" class=\"close\"><span aria-hidden=\"true\">&times;</span><span class=\"sr-only\">Close</span></button>\
                        <textarea placeholder=\"Optional Note about where your are or your schedule\"></textarea>\
                    </td>\
                </tr>;";
        $('#table').append(tr);

        $("input[type=text]").val("");
        $("input[type=tel]").val("");
        $("input[type=email]").val("");

    });
});



而我的HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>iSignout</title>

    <link type="text/css" rel="stylesheet" href="bootstrapCSS.css"/>
     <link href="css/bootstrap.min.css" rel="stylesheet"

  </head>
  <body>

    <div id="left">
            <h3 id=add>Add An Employee</h3>
            <form class="input">
                Name: <input type="text" name="name"><br>
                Phone Number: <input type="tel" name="phone"><br>
                E-Mail: <input type="email" name="email"><br>
                <input type="button" value="Add" id="submit" />
            </form>
        </div>

    <!--Creates the tables with employees -->
    <div id='table'>
        <table class= 'table table-hover'>
            <thead>
                <tr id="title"><th colspan=3>People In the Office</th></tr>
            </thead>

            <tbody>
            <!--Create rows here -->
                <tr>
                    <th>Name</th>
                    <th class>IN/OUT Status</th>
                    <th>Optional Note</th>
                </tr>

                <tr>
                    <td>
                        <a href="#openModal">Peter Griffin</a>

                            <div id="openModal" class="modalDialog">
                                <div>
                                    <a href="#close" title="Close" class="close">X</a>
                                    <h2>Peter Griffin</h2>
                                        <p>Phone:123-456-7890.</p>
                                        <p>email: petergriffin@gmail.com</p>
                                </div>
                            </div>
                    </td>
                    <td> 
                        <input type='radio' name="Peterpresent">In<br>
                        <input type='radio' name="Peterpresent">Out
                    </td>
                    <td>
                        <button type="button" class="close"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                        <textarea placeholder="Optional Note about where your are or your schedule"></textarea>
                    </td>
                </tr>

            </tbody>
        </table>
    </div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="bootstrap_script.js"></script>

  </body>
</html>

http://jsfiddle.net/36Qzy/

Answer 1:

要追加直里面你对你的新表行<div> -一个用id table 。 相反,你应该将其追加,可能在你结束<tbody> 您可以使用后代选择了这一点; 改变你append(tr)线在$('#submit').click()处理程序如下:

$('#table tbody').append(tr);


Answer 2:

更改

$('#table').append(tr);

$('#table table').append(tr);

你的表的id是DIV,而不是表本身因此被追加tr元素到div#表,而不是表本身



文章来源: Bootstrap adding rows to a table