jquery - Bootstrap 向表中添加行

标签 jquery html twitter-bootstrap

我正在尝试向我的表中动态添加行。出于某种原因,当我向表中添加一行时,它全部被揉成一团,并且没有跨越整行。为什么会发生这种情况,我该如何解决?

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

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/

最佳答案

您将新表格行直接附加到您的 <div> 中- ID 为 table 的那个.相反,你应该附加它,可能在你的 <tbody> 的末尾.您可以使用 descendant selector为了这;更改您的 append(tr)在你的 $('#submit').click() 行以下处理程序:

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

关于jquery - Bootstrap 向表中添加行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24615850/

相关文章:

javascript - jQuery:将所有元素包装在一个 div 中,但不在另一个 div 中

jquery - Cordova 2.9.0 缺少执行 :Logger. logLevel

javascript - 无法使用 JS FormData 对象发布视频文件输入

javascript - 使用 VBA 插入标签值

javascript - 使用动态数据重新加载 bootstrap3 模态

jquery - 添加 "Microsoft.jQuery.Unobtrusive.Validation"包需要 jQuery 1.4.1?

html - 使用 CSS 缩小元素的高度以匹配其父元素

javascript - 按下按键时如何以鼠标位置为中心绘制圆

css - 在 cols 中 Bootstrap 相同高度的卡片

twitter-bootstrap - Bootstrap 3 在小屏幕中折叠边距顶部有问题