jQuery(selector).contains抛出"无方法"TypeError?

vbd*_*vbd 0 javascript jquery

我的HTML:

<form>
    <input type="text" name="url" maxlength="10" />
</form>
Run Code Online (Sandbox Code Playgroud)

我的JS:

$(document).ready(function(){
    var inputUrl =  $('input[name=url]');
    inputUrl.change(function() {
        alert(inputUrl);
        if ($(inputUrl).contains('watch'))
        {
            alert ('Contains watch: yes');
        }
        else
        {
            alert ('Contains watch: no');
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

Chrome控制台显示:

Uncaught TypeError: Object [object Object] has no method 'contains'
Run Code Online (Sandbox Code Playgroud)

什么是正确的语法:

if ($(inputUrl).contains('watch'))

I H*_*azy 7

if (inputUrl.val().indexOf('watch') > -1) {
Run Code Online (Sandbox Code Playgroud)

或者您可能只想对当前接收事件的人进行操作.

if ($(this).val().indexOf('watch') > -1) {
Run Code Online (Sandbox Code Playgroud)

或者使其不区分大小写:

if (/watch/i.test($(this).val())) {
Run Code Online (Sandbox Code Playgroud)