如何检查字符串是否包含子字符串? [重复]

醉酒当歌 提交于 2020-08-09 18:52:37

问题:

This question already has an answer here: 这个问题在这里已有答案:

I have a shopping cart that displays product options in a dropdown menu and if they select "yes", I want to make some other fields on the page visible. 我有一个购物车,在下拉菜单中显示产品选项,如果他们选择“是”,我想让页面上的其他字段可见。

The problem is that the shopping cart also includes the price modifier in the text, which can be different for each product. 问题是购物车还包括文本中的价格修饰符,每个产品可能不同。 The following code works: 以下代码有效:

$(document).ready(function() {
    $('select[id="Engraving"]').change(function() {
        var str = $('select[id="Engraving"] option:selected').text();
        if (str == "Yes (+ $6.95)") {
            $('.engraving').show();
        } else {
            $('.engraving').hide();
        }
    });
});

However I would rather use something like this, which doesn't work: 但是,我宁愿使用这样的东西,这是行不通的:

$(document).ready(function() {
    $('select[id="Engraving"]').change(function() {
        var str = $('select[id="Engraving"] option:selected').text();
        if (str *= "Yes") {
            $('.engraving').show();
        } else {
            $('.engraving').hide();
        }
    });
});

I only want to perform the action if the selected option contains the word "Yes", and would ignore the price modifier. 如果所选选项包含单词“是”,我只想执行操作,并忽略价格修饰符。


解决方案:

参考一: https://stackoom.com/question/EbVT/如何检查字符串是否包含子字符串-重复
参考二: https://oldbug.net/q/EbVT/How-do-I-check-if-string-contains-substring-duplicate
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!