Javascript jquery val()在.each()之后显示为空
我正在用Javascript jquery val()在.each()之后显示为空,javascript,jquery,Javascript,Jquery,我正在用 function editRow(a) { $("td").each(function () { alert($(this).val()) }); } 它通过 $("tbody").append( "<tr id = '" + trId + "'>" + "<td name = 'nameRow' id = 'name" + idCt + "' value = 'test' > <strong>" + name + "&l
function editRow(a) {
$("td").each(function () {
alert($(this).val())
});
}
它通过
$("tbody").append(
"<tr id = '" + trId + "'>" +
"<td name = 'nameRow' id = 'name" + idCt + "' value = 'test' > <strong>" + name + "</strong> </td>" +
"<td name = 'maxRow' id = 'max" + idCt + "' value = '" + max + "'>" + max + "</td>" +
"<td name = 'setRow' id = 'sets" + idCt + "' value = '" + sets + "'>" + sets + "</td>" +
"<td name = 'repRow' id = 'reps" + idCt + "' value = '" + reps + "'>" + reps + "</td>" +
"<td name = 'restRow' id = 'rest" + idCt + "' value = '" + rest + "'>" + rest + "</td>" +
"<td id = 'link" + idCt + "'><a href='#' onclick='return deleteRow(" + trId + ");' >Remove</a>" +
" | <a href='#' onclick='return editRow(" + idCt + ");'>Edit</a></td>" +
"</tr>"
);
alter$this.val出现的次数是正确的,但它始终是空的,而不是未定义的
我知道它不是那么可读,但是有人看到错误吗
谢谢 问题是td元素没有值。它们具有内部HMTL。如果你想获得td的内容,你需要使用$this.html
function editRow(a) {
$("td").each(function () {
alert($(this).html())
});
}.val是一种仅为输入元素定义的方法。普通HTML元素没有值。您应该使用.html或.text来获取这些元素的内容
function editRow(a) {
$("td").each(function () {
alert($(this).text())
});
}
您应该为使用数据前缀定义非标准属性,如value
val方法通常为输入标记保留。对于td,您可以执行以下任一操作:
$this.text
或
$this.attr'value' 试试这样的方法
function editRow(a) {
$("td").each(function () {
alert(this.innerText);// reading inner text
alert($(this).attr('value'));// reading value attribute
});
}
啊,我明白了,谢谢你的解释。谢谢
var tdDataValue = $(this).data('value');
function editRow(a) {
$("td").each(function () {
alert(this.innerText);// reading inner text
alert($(this).attr('value'));// reading value attribute
});
}