开发者

jQuery contains not working

开发者 https://www.devze.com 2023-02-04 13:49 出处:网络
Is there something wrong with the code below it just won\'t work, no errors? var select_value = $(\"#cart-image\").attr(\'alt\');

Is there something wrong with the code below it just won't work, no errors?

var select_value = $("#cart-image").attr('alt');

if ($("select_value:contain开发者_JAVA百科s('Aqua')")) { keyword = "aqua"; };


While all the other answers are correct, they forget to mention that you can use jQuery for this. There is the Attribute contains selector:

if ($("#cart-image[alt*=Aqua]").length) keyword = "aqua";


You're using jQuery to do something that it is neither designed for nor capable of. Use Javascript's native functions to search strings for substrings:

if (select_value.indexOf('Aqua') > -1) {
    keyword = 'aqua';
}


The double quotes are making jQuery interpret the "select_value" as a string, rather than the variable.

try

if (select_value.indexOf('Aqua') != -1) { keyword = "aqua"; };


.attr() returns a string, not a DOM element

0

精彩评论

暂无评论...
验证码 换一张
取 消