jQuery-在输入上,使用跨度类获取TD内的跨度值
我正在尝试使用户更改垃圾邮件的价值(使用可满足), 通过班级。 垃圾邮件位于表中的< td>
内。
我尝试了let newitemname = $(variantid).children(“。项目 - 示例”)。text();
> 和let newitemname = $(variantid).find('span.item-description')。text();
> 两者都将一个空字符串返回到newitemname
变量。
有什么想法为什么我要获得一个空字符串而不是用户输入?
这是我的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();
});
这是我的.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值是一种颜色(即:'red')。 当用户更改该值(即:'redx')时,此代码将运行,但返回一个空字符串而不是'redx'。 谢谢!
I am trying to get the value of a spam changed by the user (using contenteditable),
through its class.
The spam is inside a <td>
in a table.
I tried let newItemName = $(variantId).children(".item-description").text();
and also let newItemName = $(variantId).find('span.item-description').text();
which both return an empty string to newItemName
variable.
Any ideas why I'm getting an empty string instead of the users input?
This is my 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)
<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 (i.e: 'red').
When the user changes that value (i.e: 'redX'), this code runs, but returns an empty string instead of 'redX'.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以再次使用
variantid
变量选择元素。但这只是一个字符串,不会选择元素,因为当使用ID选择元素时,字符串不包含#。现在
variantid
将为#+your_idYou select the element again with the
variantId
variable. 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.Now
variantId
will be #+your_id