jQuery-在输入上,使用跨度类获取TD内的跨度值

发布于 2025-02-10 12:07:43 字数 1500 浏览 1 评论 0原文

我正在尝试使用户更改垃圾邮件的价值(使用可满足), 通过班级。 垃圾邮件位于表中的< 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">&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 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.Descriptions 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭ゆ眷念 2025-02-17 12:07:43

您可以再次使用variantid变量选择元素。但这只是一个字符串,不会选择元素,因为当使用ID选择元素时,字符串不包含#。

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

现在variantid将为#+your_id

You 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.

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

Now variantId will be #+your_id

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文