【发布时间】:2020-08-27 15:09:27
【问题描述】:
问题是当我尝试通过使用 $("#[rowcount].Service_Line").change(function () { 来提取递增的 id 时,因为每个循环的 rown 计数变为 ++,它不工作的任何人都可以请帮忙,以便我可以从数据库中填充下拉列表 ** 如果我有一组 id 的行,例如 [1].Service_Line,[2].Service_Line,[3].Service_Line,.......那么我如何使用 $ 符号在 jquery 中获取它们。* *
这里是来自视图源的html代码
<table id="dataTable" border="0" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>UserName</th>
<th>Password</th>
<th>Service line</th>
<th>Track</th>
<th>subtrack</th>
<th></th>
</tr>
</thead>
<tbody>
@if (Model != null && Model.Count > 0)
{
int j = 0;
foreach (var i in Model)
{
<tr id="TemplateRow" style="border:1px solid black">
<td>@Html.TextBoxFor(a => a[j].UserName)</td>
<td>@Html.TextBoxFor(a => a[j].Password)</td>
<td>
@if (ViewBag.ServiceLineList != null)
{
@Html.DropDownListFor(a => a[j].Service_Line, ViewBag.ServiceLineList as SelectList, "--Select--", new { @id = "Service_Line", @class = "wrapper-dropdown Service_Line" })
}
</td>
<td>
@Html.DropDownListFor(a => a[j].Track, new SelectList(" "), "--Select--", new { @id="Track", @class = "wrapper-dropdown Track" })
</td>
<td>
@Html.DropDownListFor(a => a[j].Sub_Track, new SelectList(" "), "--Select--", new { @class = "wrapper-dropdown Sub_Track" })
</td>
<td>
@if (j > 0)
{
<a href="#" class="remove">Remove</a>
}
</td>
</tr>
j++;
}
}
</tbody>
</table>
jquery代码如下
$(document).ready(function () {
/* 1. Initialise our variable to keep count of the rows added */
var rowcount = 1;
//Add new row
$("#addNew").click(function (e) {
e.preventDefault();
var $tableBody = $("#dataTable");
var $trLast = $tableBody.find("tr:last");
// 2. Create the new id with the row count
var newId = "TemplateRow-" + rowcount;
// 3. clone the row with our new id
var $trNew = $trLast.clone(true).prop({ id: newId });
// 4. rename each input and give an id
$.each($trNew.find(':input'), function (i, val) {
oldName = $(this).attr('name');
inputParts = oldName.split(".");
// set the name and id with the base name and rowcount
$(this).attr('name', '[' + rowcount + '].' + inputParts[1]);
$(this).attr('id', '[' + rowcount + '].' + inputParts[1]);
$(this).removeClass("input-validation-error");
});
$("#[rowcount].Service_Line").change(function () {
$.get("/Users/GetTrackList", { Service_Line_ID: $("#[rowcount].Service_Line").val() }, function (data) {
$("#[rowcount].Track").empty();
$.each(data, function (index, row) {
$("#[rowcount].Track").append("<option value='" + row.Track_ID + "'>" + row.Track_Options + "</option>")
});
});
})
$trLast.after($trNew);
rowcount++;
});
});
问题是当我尝试使用 $("#[rowcount].Service_Line").change(function () { 它不工作可以请任何人帮助从 jquery 中提取克隆行的 ID 时
【问题讨论】:
-
不清楚你在问什么。请澄清您正在观察的具体问题。此代码中的哪个操作会产生意外结果?你有错误吗?还有什么?
-
@David Done....
-
恐怕你还没有澄清这个问题。您要读取什么具体的增量值?您具体要在哪里阅读?您如何验证该值应该存在?因为您的代码包含依赖于您的数据的服务器端代码,所以我们不能只执行您的代码来观察您所看到的内容。你需要具体。
-
@David 如何在 jquery $("#[rowcount].Service_Line") 中获得递增 id 服务行是行数作为递增值的行的 id 所以我想要的就像我有一组 id 的行,例如 [1].Service_Line,[2].Service_Line,[3].Service_Line,.......那么我如何使用 $ 符号在 jquery 中获取它们。
-
您提到的那一行存在一些问题: 1. David 已通过
rowcount确定了错误。 2. 您的 id 格式无效(例如[0].Service_Line) - id 必须 以字母开头,因此您需要更改所有 id 以满足此要求。 3. 您正在尝试查找尚未完全添加到 DOM 的元素,这可能会导致问题。
标签: html jquery ajax asp.net-mvc entity-framework