JQuery if label contains this… do this

限于喜欢 提交于 2020-01-04 05:33:30

问题


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";
        }
    });

回答1:


  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");



回答2:


.text() should work:

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



回答3:


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

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



回答4:


this you mean?

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


来源:https://stackoverflow.com/questions/3677913/jquery-if-label-contains-this-do-this

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!