Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/427.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用ajax向表中添加新行_Javascript_Asp.net_Ajax - Fatal编程技术网

Javascript 使用ajax向表中添加新行

Javascript 使用ajax向表中添加新行,javascript,asp.net,ajax,Javascript,Asp.net,Ajax,我有点问题。事实上,我意识到每次我添加一行,它都会考虑其他添加。也就是说,如果我添加一行,好的,插入到点击的下面。但是如果我想在新的下面添加,它实际上会添加两行,以此类推。有什么帮助吗?这是我的密码 <script> $(".insertRow").live('click', function () { var vref = '@Url.Action("RecordEntryRow", "Layout")'; var obj = this; $.ajax

我有点问题。事实上,我意识到每次我添加一行,它都会考虑其他添加。也就是说,如果我添加一行,好的,插入到点击的下面。但是如果我想在新的下面添加,它实际上会添加两行,以此类推。有什么帮助吗?这是我的密码

<script>


$(".insertRow").live('click', function () {

    var vref = '@Url.Action("RecordEntryRow", "Layout")';
    var obj = this;
    $.ajax({
        url: vref,
        cache: false,
        success: function (html) {
            var u = $(obj).parents(".record:first.")[0];
            $(u).after(html);
        }
    });
    return false;

});

我对你编辑的问题进行了测试。 一切正常:

<!doctype html>
<html>
    <head>
        <title>Test</title>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                var i = 0;
                $(document).off('click', '#td').on('click', 'td', function(e) {
                    $(this).parent().after('<tr><td>Append ' + i + '</td></tr>');
                    i++;
                    e.preventDefault();
                });
            });
        </script>
    </head>
    <body>
        <table>
            <tr>
                <td id="td" style="border: 1px solid red;">
                    Test
                </td>
            </tr>
        </table>
    </body>
</html>

record是表中行的类别。RecordEntryRow是我方法的名称,Layout是我控制器的名称。不,我的问题是在cliqued行下添加一行。不,在桌子的尽头。意思是说,您在这一行,当您单击加号按钮时,它会在单击的行下方添加一个新行。@JoëlMulaj'i好的,您能给我一个表格的示例吗?在这一级别,每次我添加一行,它都会添加一行。但当我想添加第二行时,他实际上拥有我之前添加的所有行,而不是添加一行,它实际上添加了之前添加的行数。WebProgrammer我添加代码作为我问题的答案。你可以在那里看到我的桌子。这对你有帮助吗?是的,事实上你错过了结尾
<tr class ="record">

@using (Html.BeginCollectionItem("Records")) { 


    <td> @Html.DropDownListFor(model => model.Type, new List<SelectListItem>{
                                                    new SelectListItem() {Text = "titre en gras", Value="Titre_Gras"},
                                                    new SelectListItem() {Text = "titre normal", Value="Titre_Normal"},
                                                    new SelectListItem() {Text = "titre minime", Value="Titre_Minime"},
                                                    new SelectListItem() {Text = "titre italique", Value="Titre_Italique"},
                                                    new SelectListItem() {Text = "item", Value="Item"},
                                                    new SelectListItem() {Text = "Separateur", Value="Separateur"},
                                                    }) </td>
    <td> @Html.EditorFor(model => model.Contenu) </td>
    <td> <a href="#" onclick="$(this).closest('tr').remove();"><img src="~/Content/images/red-delete-button.jpg" width="25" height="25" /></a></td>
    <td> <a href="#" class="insertRow" ><img src="~/Content/images/add_button.png" width="20" height="20" /></a> </td>

}