繁体   English   中英

Jquery - 在输入时,使用跨度类获取 td 内的跨度值

[英]Jquery - On input, Get the value of a span inside a td using span class

我正在尝试通过其类获取用户更改的垃圾邮件的值(使用 contenteditable)。 垃圾邮件位于表中的<td>内。

我试过let newItemName = $(variantId).children(".item-description").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">&nbsp;&nbsp;&nbsp;</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的值是一种颜色(即:“红色”)。 当用户更改该值(即:'redX')时,此代码运行,但返回一个空字符串而不是 'redX'。 谢谢!

您使用variantId变量再次选择元素。 但这只是一个字符串,不会选择元素,因为在选择具有 id 的元素时,该字符串不包含 #。

variantId = '#'+$(this).attr('id');

现在variantId将是 #+your_id

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM