Html 如何提取jquery中元素的递增id
问题是,当我尝试使用$(“#[rowcount].Service_Line”).change(函数(){随着rown计数的增加++对于每个不起作用的循环,请任何人提供帮助,以便我可以填充数据库中的下拉列表 **如果我有一组id为[1]的行,比如[1]。服务行[2]。服务行[3]。服务行,…..,那么如何使用$symbol在jquery中获取它们** 下面是来自视图源代码的html代码Html 如何提取jquery中元素的递增id,html,jquery,ajax,asp.net-mvc,entity-framework,Html,Jquery,Ajax,Asp.net Mvc,Entity Framework,问题是,当我尝试使用$(“#[rowcount].Service_Line”).change(函数(){随着rown计数的增加++对于每个不起作用的循环,请任何人提供帮助,以便我可以填充数据库中的下拉列表 **如果我有一组id为[1]的行,比如[1]。服务行[2]。服务行[3]。服务行,…..,那么如何使用$symbol在jquery中获取它们** 下面是来自视图源代码的html代码 <table id="dataTable" border="0"
<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>
用户名
密码
服务线
跟踪
子轨
@if(Model!=null&&Model.Count>0)
{
int j=0;
foreach(模型中的var i)
{
@Html.TextBoxFor(a=>a[j].UserName)
@Html.TextBoxFor(a=>a[j].密码)
@如果(ViewBag.ServiceLineList!=null)
{
@Html.DropDownListFor(a=>a[j].Service\u行,ViewBag.ServiceLineList作为SelectList,“--Select--”,新建{@id=“Service\u行”,“@class=”wrapper dropdown Service\u行“})
}
@Html.DropDownListFor(a=>a[j].Track,new SelectList(“”,“--Select--”,new{@id=“Track”,@class=“wrapper dropdown Track”})
@Html.DropDownListFor(a=>a[j].Sub_Track,new SelectList(“”,“--Select——”,new{@class=“wrapper dropdown Sub_Track”})
@如果(j>0)
{
}
j++;
}
}
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++;
});
});
$(文档).ready(函数(){
/*1.初始化变量以保持添加的行数*/
var rowcount=1;
//添加新行
$(“#添加新”)。单击(函数(e){
e、 预防默认值();
var$tableBody=$(“#数据表”);
var$trLast=$tableBody.find(“tr:last”);
//2.使用行计数创建新id
var newId=“TemplateRow-”+行数;
//3.使用新id克隆行
var$trNew=$trLast.clone(true).prop({id:newId});
//4.重命名每个输入并给出一个id
$.each($trNew.find(':input'),函数(i,val){
oldName=$(this.attr('name');
inputParts=oldName.split(“.”);
//使用基本名称和行数设置名称和id
$(this.attr('name','['+rowcount+'].+inputParts[1]);
$(this.attr('id','['+rowcount+'].+inputParts[1]);
$(this).removeClass(“输入验证错误”);
});
$(“#[rowcount].Service_行”).change(函数(){
$.get(“/Users/GetTrackList”,{Service_Line_ID:$(“#[rowcount].Service_Line”).val()},函数(数据){
$(“#[rowcount].Track”).empty();
$.each(数据、函数(索引、行){
$(“#[rowcount].Track”).append(“+row.Track_Options+”)
});
});
})
$trLast.after($trNew);
行计数++;
});
});
问题是,当我尝试使用$(“#[rowcount].Service_Line”)从jquery中提取克隆行的id时。更改(函数(){它不起作用,请任何人帮助注意您如何在字符串中使用变量
rowcount
:
'[' + rowcount + '].'
"#[rowcount].Service_Line"
请注意,您是如何尝试在字符串中使用它的:
'[' + rowcount + '].'
"#[rowcount].Service_Line"
看到区别了吗?在后一个示例中,您实际上并没有使用rowcount
变量,您只是有一个包含文本“rowcount”
的字符串。请像在前一个示例中一样使用该变量:
"#[" + rowcount + "].Service_Line"
(对要在字符串中使用变量的任何其他位置重复此操作。)
您甚至可以使用较新的语法:
(请注意,堆栈溢出上此处突出显示的语法当前未正确突出显示此语法。)
但在这两种语法中,关键的区别在于使用变量与仅使用带有变量名称的字符串。不清楚您在问什么。请澄清您观察到的具体问题。此代码中的哪个操作产生了意外的结果?您是否有错误?其他问题?@David Done…我很抱歉恐怕您还没有澄清这个问题。您试图读取的具体递增值是什么?您试图读取的具体位置是什么?您是如何验证该值应该存在的?因为您的代码包括依赖于您的数据的服务器端代码,我们不能仅执行您的代码来观察您看到的内容。您需要具体说明。@David我如何在jquery$(“#[rowcount].Service_行”)中获取递增id服务行是以rowcount作为递增值的行的id,因此我想要的是一组id类似于[1]的行。服务行[2]。服务行[3].Service_Line,…..那么我如何使用$symbol在jquery中获取它们。您提到的行有几个问题:1.David已识别出
行数的错误。2.您的id格式无效(例如[0]。Service_Line
)-id必须以字母开头,因此您需要更改所有id以满足此要求。3.您正在尝试查找尚未完全添加到DOM的元素,这可能会导致问题。请您帮助我尝试dis几天,但无法解决此问题,我将非常感谢您