根据选项元素中的文本进行选项选择 [英] Make option selection based off text in option element

查看:64
本文介绍了根据选项元素中的文本进行选项选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在下拉列表中选择具有传递给函数的文本值的选项,获取它的值,然后选择具有该值的选项。你能明白为什么这不起作用吗?

I am attempting to select the option in a dropdown list that has the text value passed into the function, get it's value, and then select the option with that value. Can you see why the this isn't working?

  function selectAndAddToCart(value)
    {
        console.log('The selectAndAddToCart onclick value is ' + value);
        var optionToSelect = $j('#attribute136').find('option[text="' + value + '"]').val(); //select the option with the node text that equals value
//select the option with the node text that equals value
        var vals = $j('#attribute136').val() || [];
        vals.push(optionToSelect);
        $j('#attribute136').val(vals);

        //initiate add to cart function
        productAddToCartForm.submit(this); 
    }


推荐答案

看起来你正在使用noConflict,将 $ 重命名为 $ j ,但是在使用 $ <的失败选择器上/ code>,而不是 $ j

It looks like you are using noConflict, renaming $ to $j, yet on the failing selector you are using $, and not $j!

我会这样做:

var optionToSelect = $j('option', '#attribute136').filter(function() {
    return $(this).text().indexOf(value) != -1;
}).val();

但这只是因为我不太关心 contains() 选择器:

But that's only because I don't much care for the contains() selector:

$("#attribute136 option:contains(" + value + ")");

这篇关于根据选项元素中的文本进行选项选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆