jquery val() contains()

2019-04-03 18:16发布

I want to know if the textarea value contains a certain word. This is not working for me.

var value = $('#embedModal textarea').val();
if($(value).contains('iframe')){...

5条回答
何必那么认真
2楼-- · 2019-04-03 18:25

Try javascript

if (value.indexOf('iframe') >= 0) {

JQuery contains is for DOM elements, not strings.

查看更多
看我几分像从前
3楼-- · 2019-04-03 18:30

Try following code.....

code:

             <textarea rows="4" cols="9">Something iframe</textarea>
             <textarea rows="4" cols="9">Something</textarea>

               var obj=$("textarea:contains('iframe')");

                   if(obj){

                            obj.css("background-color", "red");

                         }
查看更多
老娘就宠你
4楼-- · 2019-04-03 18:34

You can do this as well:

if($('#embedModal textarea:contains("iframe")').length > 0){
    //TODO: do stuff
}
查看更多
Fickle 薄情
5楼-- · 2019-04-03 18:35

this works:

$.contains( document.documentElement, document.body ); // true

var babyEl = $('div#id');
var daddyEl = $('div#ID');

if($.contains(babyEl, daddyEl)) {
    //do fun stuff
}
查看更多
手持菜刀,她持情操
6楼-- · 2019-04-03 18:43

Try doing it like this:

$('#embedModal textarea:contains("iframe")').each(function() {
  //Do something
});

edit

Example

查看更多
登录 后发表回答