【问题标题】:Form Element Appended from Ajax not working-从 Ajax 附加的表单元素不起作用-
【发布时间】:2022-01-12 03:01:43
【问题描述】:

我在 ajax 中有这个表单方法的代码

 $.ajax({
    type: "GET",
    url: "/MainPage/GetAllRecords",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function (data) {
        console.log(data);
        $.each(data, function (key, value) {
            
            $("#loadNotes").append(
                '<form method="GET">' +
                '<div class="container py-3 px-4" style = "background-color: #fff;position:relative; height: 8rem; border: #80808012 solid 1px; /* margin-top: 1rem; */ box-shadow: blue; margin-bottom: 7px; overflow: hidden; text-overflow: ellipsis;  /* overflow-wrap: break-word; */ ">' +

                '<a class="stretched-link" asp-controller="MainPage" asp-action="ShowRecord" asp-route-id="' + value.id + '"style = "color: #6d1cccbf; font-weight: 500; text-decoration:underline;" type="submit">' + value.title + '</a>' +
                '<p style="color: #5b5454;font-size: 11pt;height: 3rem; white-space:unset" class="text-break text-truncate">' + value.description + '</p>' +

                '</div>' +
                '</form>'
               ) ;
        });
    },
    error: function (response) {
        alert(response);
    }
});

我已经尝试过将表单方法和其他元素放在 cshtml 文件下,然后从那里它可以工作,它正在调用控制器并查看但不是从 ajax。

我错过了什么?请帮忙谢谢

【问题讨论】:

  • 嗨@devdeb26,您的视图现在从ajax 显示什么? Console 面板(浏览器中的 F12)是否出现任何错误消息?你能分享你的回复数据吗?另外,分享您的后端代码。
  • 此外,标签助手被解释。换句话说,Razor 必须将它们视为实际标签才能替换它们。在这里,它只是一个 JS 字符串,Razor 不会乱用它。所以它不会通过js生成href。

标签: javascript html asp.net ajax asp.net-core


【解决方案1】:

这是一个完整的工作演示:

型号:

public class TestModel
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
}

查看:

<div id=loadNotes>

</div>
@section Scripts
{
    <script>
        $(function(){
            $.ajax({
                type: "GET",
                url: "/MainPage/GetAllRecords",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    console.log(data);
                    $.each(data, function (key, value) {
            
                        $("#loadNotes").append(
                            '<form method="GET">' +
                            '<div class="container py-3 px-4" style = "background-color: #fff;position:relative; height: 8rem; border: #80808012 solid 1px; /* margin-top: 1rem; */ box-shadow: blue; margin-bottom: 7px; overflow: hidden; text-overflow: ellipsis;  /* overflow-wrap: break-word; */ ">' +
                //change here.......    
                            '<a class="stretched-link" href="/MainPage/ShowRecord/' + value.id + '"style = "color: #6d1cccbf; font-weight: 500; text-decoration:underline;" type="submit">' + value.title + '</a>' +
                            '<p style="color: #5b5454;font-size: 11pt;height: 3rem; white-space:unset" class="text-break text-truncate">' + value.description + '</p>' +

                            '</div>' +
                            '</form>'
                            ) ;
                    });
                },
                error: function (response) {
                    alert(response);
                }
            });
        })
         
    </script>
}

控制器:

public class MainPageController : Controller
{
    public IActionResult GetAllRecords()
    {
        //hard-coded the data for easy testing
        var model = new List<TestModel>()
        {
            new TestModel(){Id=1,Title="aa",Description="des1"},
            new TestModel(){Id=2,Title="bb",Description="des2"},
            new TestModel(){Id=3,Title="cc",Description="des3"}
        };
        return Json(model);
    }
}

注意:

标签助手被解释。换句话说,Razor 必须将它们视为实际标签才能替换它们。在这里,它只是一个 JS 字符串,Razor 不会乱用它。所以它不会通过js生成href。

您需要手动设置 href 值。把js改成:

'<a class="stretched-link" href="/MainPage/ShowRecord/' + value.id + '"style = "color: #6d1cccbf; font-weight: 500; text-decoration:underline;" type="submit">' + value.title + '</a>' +

【讨论】:

  • 谢谢!这行得通!非常感谢您的解释。我现在知道为什么我的 taghelpers 不在 ajax 上工作了!祝福你
【解决方案2】:

您作为 GET 传递的 type 属性应该是我认为的方法。

剩下的代码似乎没问题。您检查浏览器控制台是否有错误?

【讨论】:

  • 开发工具中没有控制台错误,我的本地解决方案也没有错误:(
【解决方案3】:

使用反引号``

$("#loadNotes").append(`
    <form method="GET">
        <div class="container py-3 px-4" style = "background-color: #fff;position:relative; height: 8rem; border: #80808012 solid 1px; box-shadow: blue; margin-bottom: 7px; overflow: hidden; text-overflow: ellipsis; ">
           <a class="stretched-link" asp-controller="MainPage" asp-action="ShowRecord" asp-route-id="` + value.id + `"style = "color: #6d1cccbf; font-weight: 500; text-decoration:underline;" type="submit"> ` + value.title + `</a>
           <p style="color: #5b5454;font-size: 11pt;height: 3rem; white-space:unset" class="text-break text-truncate">` + value.description + `</p>
        </div>
    </form>
`);

【讨论】:

  • 使用反引号``
  • 您好,我刚刚尝试了您的建议,但仍未将我引导至控制器视图。我在开发工具上也没有控制台错误,与我本地的一样
  • @devdeb26 检查我的编辑。
【解决方案4】:

也许先将 html“表单标签”存储在变量中,然后使用 .html() 将表单附加到#loadNotes

并确保在“#loadNotes”之后添加(文档)以绝对找到文档中的元素。 $("#loadNotes", document)

还有成功:函数和错误:函数有点旧。尝试 .done 和 .fail

Ajax 的旧方法。

 $.ajax({
    type: "GET",
    url: "/MainPage/GetAllRecords",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function (data) {
        console.log(data);
        var html_forms = '';
        $.each(data, function (key, value) {
                html_forms += '<form method="GET">' +
                '<div class="container py-3 px-4" style = "background-color: #fff;position:relative; height: 8rem; border: #80808012 solid 1px; /* margin-top: 1rem; */ box-shadow: blue; margin-bottom: 7px; overflow: hidden; text-overflow: ellipsis;  /* overflow-wrap: break-word; */ ">' +
                '<a class="stretched-link" asp-controller="MainPage" asp-action="ShowRecord" asp-route-id="' + value.id + '"style = "color: #6d1cccbf; font-weight: 500; text-decoration:underline;" type="submit">' + value.title + '</a>' +
                '<p style="color: #5b5454;font-size: 11pt;height: 3rem; white-space:unset" class="text-break text-truncate">' + value.description + '</p>' +
                '</div>' +
                '</form>';
        });

        $("#loadNotes", document).html(html_forms);
    },
    error: function (response) {
        alert(response);
    }
});

Ajax 的新方法

 $.ajax({
    type: "GET",
    url: "/MainPage/GetAllRecords",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
}).done(function(data) {
        console.log(data);
        var html_forms = '';
        $.each(data, function (key, value) {
                html_forms += '<form method="GET">' +
                '<div class="container py-3 px-4" style = "background-color: #fff;position:relative; height: 8rem; border: #80808012 solid 1px; /* margin-top: 1rem; */ box-shadow: blue; margin-bottom: 7px; overflow: hidden; text-overflow: ellipsis;  /* overflow-wrap: break-word; */ ">' +
                '<a class="stretched-link" asp-controller="MainPage" asp-action="ShowRecord" asp-route-id="' + value.id + '"style = "color: #6d1cccbf; font-weight: 500; text-decoration:underline;" type="submit">' + value.title + '</a>' +
                '<p style="color: #5b5454;font-size: 11pt;height: 3rem; white-space:unset" class="text-break text-truncate">' + value.description + '</p>' +
                '</div>' +
                '</form>';
        });

        $("#loadNotes", document).html(html_forms);
}).fail(function(response) {
       alert(response);
});

编辑:将此部分更改为&lt; button

从这里

'<a class="stretched-link" asp-controller="MainPage" asp-action="ShowRecord" asp-route-id="' + value.id + '"style = "color: #6d1cccbf; font-weight: 500; text-decoration:underline;" type="submit">' + value.title + '</a>'

到这里

'<button class="stretched-link" asp-controller="MainPage" asp-action="ShowRecord" asp-route-id="' + value.id + '"style = "color: #6d1cccbf; font-weight: 500; text-decoration:underline;" type="submit">' + value.title + '</button>'

并为此添加操作

html_forms += '<form action="php_controller.php" method="GET">' +

【讨论】:

  • 是的,我将旧的 ajax 更新为新的,但它仍然附加了我的 html 表单,不幸的是,每当我单击链接时,它都不会调用控制器和操作。简而言之,单击它后它什么也没做。 :(
  • @devdeb26 检查我的编辑。
  • 您好,谢谢!这不起作用,但它帮助我知道已经有一种新的 ajax 格式,从现在开始我将一直使用它。感谢您的帮助!
猜你喜欢
  • 2020-09-11
  • 2013-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-06
  • 2012-05-24
  • 2014-04-16
  • 1970-01-01
相关资源
最近更新 更多