[英]Jquery - On input, Get the value of a span inside a td using span class
I am trying to get the value of a spam changed by the user (using contenteditable), through its class.我正在尝试通过其类获取用户更改的垃圾邮件的值(使用 contenteditable)。 The spam is inside a <td>
in a table.垃圾邮件位于表中的<td>
内。
I tried let newItemName = $(variantId).children(".item-description").text();
我试过let newItemName = $(variantId).children(".item-description").text();
and also let newItemName = $(variantId).find('span.item-description').text();
并let newItemName = $(variantId).find('span.item-description').text();
which both return an empty string to newItemName
variable.它们都向newItemName
变量返回一个空字符串。
Any ideas why I'm getting an empty string instead of the users input?任何想法为什么我得到一个空字符串而不是用户输入?
This is my JS:这是我的 JS:
let variantId;
$(document).on('click', ".tbl-itemClass", function () {
variantId = $(this).attr('id');
$(variantId).prop('contenteditable', true);
}).
on('input', function () {
//let newItemName = $(variantId).children(".item-description").text();
let newItemName = $(variantId).find('span.item-description').text();
});
This is my .csHtml (don't think it makes a difference, I'm using razor page, Asp.Net)这是我的 .csHtml (不要认为这有什么区别,我使用的是剃须刀页面,Asp.Net)
<tbody>
@foreach (var item in Model)
{
<tr currentitemid=" @item.Id">
<td class="tbl-itemClass desc" id="@("description"+ item.Id)" varianttype="@ViewBag.VariantType" >
<span class="spnSpc"> </span>
<span style="width:90%; padding:1px 5px 1px 2px;" **class="item-description"** contenteditable> @item.Description </span>
</td>
<td class="tbl-itemClass ">//more code here
</td>
</tr>
}
@item.Description
s value is a color (ie: 'red'). @item.Description
的值是一种颜色(即:“红色”)。 When the user changes that value (ie: 'redX'), this code runs, but returns an empty string instead of 'redX'.当用户更改该值(即:'redX')时,此代码运行,但返回一个空字符串而不是 'redX'。 Thanks!谢谢!
You select the element again with the variantId
variable.您使用variantId
变量再次选择元素。 But that is only a string and won't select the element, because the string doesn't contain the # when selecting elements with an id.但这只是一个字符串,不会选择元素,因为在选择具有 id 的元素时,该字符串不包含 #。
variantId = '#'+$(this).attr('id');
Now variantId
will be #+your_id现在variantId
将是 #+your_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.