简体   繁体   English

HTML表格格式错误。 当元素应该垂直放置时,为什么要创建楼梯呢?

[英]HTML Table formatting bug. Why are elements creating a staircase, when they should be going vertical?

I am creating a form which adds 2 textboxes on the click of "add material", one to represent a material, one for a URL. 我正在创建一个窗体,该窗体在“添加材料”的单击上添加2个文本框,一个代表材料,一个代表URL。

When I click it however, it seems to make a staircase display, rather than a vertical, 2 row display. 但是,当我单击它时,它似乎是在显示楼梯,而不是垂直的两行显示。 I'm new to Javascript, please help! 我是Java语言的新手,请帮助!

HTML/JS: HTML / JS:

<form role="form" method="post">
                    <table>
                        <tr>
                            <td>
                                <label for="title">Project Title</label>
                                <input type="text" name="title" value="">
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <label for="description">Description</label>
                                <textarea name="description"></textarea>
                            </td>
                        </tr>
                    </table>
                    <table class="materialstab" style="border:1px solid;" cellpadding="5">
                        <script type="text/javascript">
                        jQuery(document).ready(function($){
                            $('.my-form .add-box').click(function(){
                                var n = $('.text-box').length + 1;

                                var box_html = $('<tr><td><p class="text-box"><input type="text" name="materials" value="" id="materials' + n + '" /></p></td><td><p class="text-box"> <input type="text" name="url' + n + '" value="" id="url' + n + '" /><a href="#" class="remove-box">Remove</a></p></td></tr>');
                                box_html.hide();
                                $('.my-form .materialstab p.text-box:last').after(box_html);
                                box_html.fadeIn('slow');
                                return false;
                            });
                            $('.my-form').on('click', '.remove-box', function(){
                                $(this).parent().css( 'background-color', '#FF6C6C' );
                                $(this).parent().fadeOut("slow", function() {
                                    $(this).remove();
                                    $('.box-number').each(function(index){
                                        $(this).text( index + 2 );
                                    });
                                });
                                return false;
                            });
                        });
                        $(document).ready( function () {

                            var nbtextbox = $('input[name="materials"]').length;
                            var box_2 = $('<input type="hidden" name="elem1" value="'+nbtextbox+'">');
                            $('.my-form p.text-box:last').after(box_2);

                       });
                        </script>
                        <tr>
                            <td>
                                <p class="text-box">
                                    <label for="materials">Materials</label>
                                </p>
                            </td>
                            <td>
                                <p class="text-box">
                                    <label for="url">URL</label>
                                    <a class="add-box" href="#">Add Material</a>
                                </p>
                            </td>
                        </tr>

                    </table>

To fix this, first of all move the <script> element outside the table. 要解决此问题,首先将<script>元素移到表外。 It doesn't need to be in there. 它不需要在那里。 Then add the class you're using to your <form> : 然后将您正在使用的类添加到<form>

<form class="my-form" role="form" method="post">

Finally, change the selector in the 7th line of your script to 最后,将脚本第7行中的选择器更改为

$('.my-form .materialstab tr:last')

 jQuery(document).ready(function() { $('.my-form .add-box').click(function() { var n = $('.text-box').length + 1; var box_html = $('<tr><td><p class="text-box"><input type="text" name="materials" value="" id="materials' + n + '" /></p></td><td><p class="text-box"> <input type="text" name="url' + n + '" value="" id="url' + n + '" /><a href="#" class="remove-box">Remove</a></p></td></tr>'); box_html.hide(); $('.my-form .materialstab tr:last').after(box_html); box_html.fadeIn('slow'); return false; }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form class="my-form" role="form" method="post"> <table class="materialstab" style="border:1px solid;" cellpadding="5"> <tr> <td> <p class="text-box"> <label for="materials">Materials</label> </p> </td> <td> <p class="text-box"> <label for="url">URL</label> <a class="add-box" href="#">Add Material</a> </p> </td> </tr> </table> </form> 

You want to add you new rows to the table, not the end of the previous row (inside the <tr> tag.) 您想向表中添加新行,而不是前一行的末尾(在<tr>标记内)。

$('.my-form .materialstab').append(box_html);

In the remove eventhandler you need to be doing your work on the <tr> element. 在remove eventhandler中,您需要在<tr>元素上进行工作。 this is bound to the <a> element. this绑定到<a>元素。

$('.my-form').on('click', '.remove-box', function(){
    var $row = $(this).parents('tr');
    $row.css( 'background-color', '#FF6C6C' );
    $row.fadeOut("slow", function() {
        $row.remove();
        $('.box-number').each(function(index){
            $(this).text( index + 2 );
        });
    });
    return false;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM