JQuery 如果标签包含这个...执行此操作

发布于 2024-09-18 14:21:28 字数 377 浏览 4 评论 0原文

我的页面上有 asp.net 中继器。如果重复的每个项目都包含在一个标签中,如下所示:

<label class="ItemName">value</label>

如果该标签包含文本“35”,我想在其旁边显示一些文本。我怎样才能使用 jquery 做到这一点???

    jQuery(document).ready(function () {
        if ($('.ItemName').val().indexOf("35")) {
            $(this).val() = $(this).val() + "some text";
        }
    });

I have asp.net repeater on a page. If each item being repeated is wrapped in a label like so:

<label class="ItemName">value</label>

If this label contains the text '35' I want to display some text next to it. How can i do this using jquery???

    jQuery(document).ready(function () {
        if ($('.ItemName').val().indexOf("35")) {
            $(this).val() = $(this).val() + "some text";
        }
    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

自由如风 2024-09-25 14:21:28
  1. .ready 函数中的 this 应引用 document
  2. 要获取文本内容,请使用 .text() 而不是 .val()
  3. 要更新某些值,请使用 $obj.val(blah);,而不是 $obj.val() = blah;。 (这实际上是Javascript的限制。)
  4. 有一个 :contains() 选择器 过滤包含某些文本的元素。
  5. 要附加一些文本(或 HTML),已经有 一个 .append() 方法(感谢@JP提醒这一点。)

您可能需要这个:

$('.ItemName:contains(35)').append("some text");
  1. The this in the .ready function should refer to the document.
  2. To get the text content, use .text() instead of .val().
  3. To update some value, use $obj.val(blah);, not $obj.val() = blah;. (This is actually a limitation of Javascript.)
  4. There is a :contains() selector to filter elements containing some text.
  5. To append some text (or HTML), there is already an .append() method (Thanks @J-P for reminding this.)

You may want this instead:

$('.ItemName:contains(35)').append("some text");
风苍溪 2024-09-25 14:21:28

.text() 应该可以工作:

var item = $('.ItemName');
if ( item.text().indexOf("35") > -1 ) {
    item.after("some text");
}

.text() should work:

var item = $('.ItemName');
if ( item.text().indexOf("35") > -1 ) {
    item.after("some text");
}
北方的巷 2024-09-25 14:21:28

indexOf 如果未找到则返回 -1,否则返回索引。这样做:

if ($('.ItemName').val().indexOf("35") >= 0) {

indexOf returns -1 if not found, or the index. Do this:

if ($('.ItemName').val().indexOf("35") >= 0) {
你与昨日 2024-09-25 14:21:28

你的意思是这个?

jQuery(document).ready(function () {
        if ($('.ItemName').text().indexOf("35")) {
            $(this).text($(this).text() + "some text");
        }
    });

this you mean?

jQuery(document).ready(function () {
        if ($('.ItemName').text().indexOf("35")) {
            $(this).text($(this).text() + "some text");
        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文