gpt4 book ai didi

javascript - 如何更改 Ajax 生成的 HTML 内容

转载 作者:行者123 更新时间:2023-11-28 05:12:15 24 4
gpt4 key购买 nike

您好,我在更改由 AJAX 生成的 HTML 内容时遇到问题

这是页面的代码:

@model IEnumerable<WE_SRW_PeerNissen.Models.Reservations>

@{
ViewBag.Title = "List";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Übersicht Liste</h2>


<form method="post" action="List">
<div class="input-group">
<div class="input-group-btn">
<input type="text" class="form-control" name="searchbar" placeholder="Search">

<button class="btn btn-default" type="submit">
<i class="glyphicon glyphicon-search"></i>
</button>
</div>
</div>
</form>

<table class="table" id="sorttable">
<thead bgcolor="white">
<tr>
<th>
@Html.DisplayNameFor(model => model.BookingNr)
</th>
<th>
@Html.DisplayNameFor(model => model.Arrival)
</th>
<th>
@Html.DisplayNameFor(model => model.Departure)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Appartment)
</th>
<th>
@Html.DisplayNameFor(model => model.Adult)
</th>
<th>
@Html.DisplayNameFor(model => model.Children)
</th>
<th>
@Html.DisplayNameFor(model => model.Total)
</th>
</tr>

</thead>

</table>
<link href="~/Content/DataTables/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="~/Scripts/DataTables/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function () {
$('#sorttable').DataTable({
"ajax": {
"url": "/OverView/loaddata",
"type": "GET",
"datatype": "json"
},
"columns": [
{ "data": "BookingNr", "autoWidth": true },
{ "data": "Arrival", "autoWidth": true },
{ "data": "Departure", "autoWidth": true },
{ "data": "Name", "autoWidth": true },
{ "data": "Appartment", "autoWidth": true },
{ "data": "Adult", "autoWidth": true },
{ "data": "Children", "autoWidth": true },
{ "data": "Total", "autoWidth": true },
]
});
});

</script>

生成的示例:

<div class="dataTables_filter" id="sorttable_filter">
<label>Search:
<input aria-controls="sorttable" type="search" placeholder="">
</label>
</div>

我认为这是我最好的尝试:

<script>
$(document).ready(function () {
$(document).on(function () {
$('#sorttable_filter label:eq(1)').text('Suchen:');
});
});
</script>

我在 AJAX 脚本下方设置,我还尝试将其放在下面相同的脚本标签中,以确保 AJAX 已执行

最佳答案

一些评论...

jQuery on() 方法将函数绑定(bind)到特定事件,例如: $(something).on("click", doSomething) 并且您只提供了一个函数,因此它不知道将函数绑定(bind)到什么事件.

然后,eq(1) 是 jQuery 方法,但不是 CSS 选择器,因此您需要使用“label:first-child”(如果您需要第一个)。但是,它会破坏示例 HTML 中的输入字段,因为它位于标签元素内部,因此您(可能)希望将其放在外部。如果您无法更改 HTML 输出,您可以使用“移动”标签元素之外的输入副本的肮脏技巧......就像这样

$(document).ready(function() {
var labelEl = document.querySelector("#sorttable_filter label:first-child");
var inputEl = labelEl.querySelector("input");
labelEl.parentNode.appendChild(inputEl);
labelEl.innerText = 'Suchen:';
});

我不喜欢的是 document.ready() 可能会在实际元素出现之前一段时间发生,因此您至少需要查找结果 HTML 外观...

关于javascript - 如何更改 Ajax 生成的 HTML 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41264804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com