我试图遍历一个表列并建立一个字符串。
这是我到目前为止所拥有的:
$(".ConvertSQL").click(function () {
var values
$(".TableQuery tr").each(function () {
$('td', this).each(function () {
values = value + ',' + value;
alert(values)
})
})
});
收到错误:
我的警报显示一条消息,提示“未定义” ,并不断重复此操作次数,直到单元格中有数据为止
我还是这个新手,所以不完全了解发生了什么。
此刻的设置不会遍历一列,我认为它会遍历表中的每个单元格,因此也需要更改它
答案 0 :(得分:1)
您的逻辑问题是,尽管您定义了// Binding our UserPositionsRequest class
public void Index(UserPositionsRequest request) {
// Checking if we should update, if you will change the request to boolean type: "true"
// ..on the client side, then you could actually change the condition to be: if (request.Update)
if (request.Update == 1) {
// Creating database connection using (I assume) EntityFramework
using (var picturesEntities = new picturesEntities()) {
// Building a dictionary for fast lookup. Key, Value as the 0, 1 arg respectfully
var usersDataToUpdate = request.Positions.ToDictionary(p => p[0], p => p[1]);
// Finding the entries that needs to be updated
var usersEntitiesToUpdate = picturesEntities.userTables.Where(cntry => usersDataToUpdate.ContainsKey(cntry.Id));
// Iterating over the entities
foreach (var userEntity in usersEntitiesToUpdate) {
// Updating their position.
userEntity.Position = usersDataToUpdate[userEntity.Id];
}
picturesEntities.SaveChanges();
}
}
// Probably you wanted to return something here, but it's probably an ajax and you can skip that.
}
,但从未真正将其设置为保存任何值,因此它仍然为value
。
从代码的上下文来看,您似乎正在尝试从undefined
元素中获取文本内容,因此可以将td
方法与text()
一起使用来获取所需列的:nth-child()
。
还请注意,要使此操作更容易,您可以使用td
构建一个值数组,然后map()
形成单个字符串。试试这个:
join()
$(".ConvertSQL").click(function() {
var str = $(".TableQuery td:nth-child(1)").map((i, e) => $(e).text()).get().join(' ');
console.log(str);
});