[英]Jquery - On input, Get the value of a span inside a td using span class
[英]JQuery: Assigning value to td span if span is empty
我有一个函数连接到数据库的表。 如果存在玩家得分,则将值分配给td跨度。 如果没有玩家得分,则不会为td跨度分配任何值。 如果分数不存在或td跨度为空,则尝试用“ 0”填充跨度。
$("table tbody tr").children('td').find("span.playerScore").each(function () {
var test = $(this).text();
if (test === "") {
$(this).parent("td").children('span').text() = "0";
}
})
这不起作用。 我绝对觉得这很明显,而且我只是瞎子,所以可以提供任何帮助。 谢谢。
这个...
$(this).parent("td").children('span').text() = "0";
不是设置元素文本的方式。
您需要将文本作为参数传递给text
:
$(this).parent("td").children('span').text("0");
使用:empty
选择器可能会更容易。 这样可以节省几行代码。
重要的是要注意,文本的值必须用括号括起来。
$("table tbody tr").children('td').find("span.playerScore").filter(":empty").each(function () {
$(this).parent("td").children('span').text("0");
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.