Jor*_*ris 966 javascript string jquery substring contains
我有一个购物车,在下拉菜单中显示产品选项,如果他们选择"是",我想在页面上显示其他一些字段.
问题是购物车还包括文本中的价格修饰符,每个产品可能不同.以下代码有效:
$(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();
}
});
});
Run Code Online (Sandbox Code Playgroud)
但是,我宁愿使用这样的东西,这是行不通的:
$(document).ready(function() {
$('select[id="Engraving"]').change(function() {
var str = $('select[id="Engraving"] option:selected').text();
if (str *= "Yes") {
$('.engraving').show();
} else {
$('.engraving').hide();
}
});
});
Run Code Online (Sandbox Code Playgroud)
如果所选选项包含单词"是",我只想执行操作,并忽略价格修饰符.
SLa*_*aks 2076
像这样:
if (str.indexOf("Yes") >= 0)
Run Code Online (Sandbox Code Playgroud)
...或者你可以使用代字号运算符:
if (~str.indexOf("Yes"))
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为如果根本找不到字符串则indexOf()返回-1.
请注意,这是区分大小写的.
如果您想要不区分大小写的搜索,可以编写
if (str.toLowerCase().indexOf("yes") >= 0)
Run Code Online (Sandbox Code Playgroud)
要么,
if (/yes/i.test(str))
Run Code Online (Sandbox Code Playgroud)
hoo*_*ter 117
str.search( 'Yes' )
将返回匹配的位置,如果未找到则返回-1.
And*_*ham 27
其他方式:
var testStr = "This is a test";
if(testStr.contains("test")){
alert("String Found");
}
Run Code Online (Sandbox Code Playgroud)
**在Firefox,Safari 6和Chrome 36上测试**
Mun*_*osh 27
写这个答案已经很晚了,但无论如何我想要包括它.String.prototype现在有一个方法includes可以检查子串.此方法区分大小写.
var str = 'It was a good date';
console.log(str.includes('good')); // shows true
console.log(str.includes('Good')); // shows false
Run Code Online (Sandbox Code Playgroud)
要检查子字符串,可以采用以下方法:
if (mainString.toLowerCase().includes(substringToCheck.toLowerCase())) {
// mainString contains substringToCheck
}
Run Code Online (Sandbox Code Playgroud)
查看文档以了解更多信息.
Ori*_*iol 14
ECMAScript 6介绍String.prototype.includes,以前命名contains.
它可以像这样使用:
'foobar'.includes('foo'); // true
'foobar'.includes('baz'); // false
Run Code Online (Sandbox Code Playgroud)
它还接受一个可选的第二个参数,它指定开始搜索的位置:
'foobar'.includes('foo', 1); // false
'foobar'.includes('bar', 1); // true
Run Code Online (Sandbox Code Playgroud)
它可以被polyfilled以使其在旧浏览器上工作.
小智 9
您可以在ie和chrome中使用此Polyfill
if (!('contains' in String.prototype)) {
String.prototype.contains = function (str, startIndex) {
"use strict";
return -1 !== String.prototype.indexOf.call(this, str, startIndex);
};
}
Run Code Online (Sandbox Code Playgroud)
返回关键字包含在字符串中的次数.
var count = "I have one keyword".match(/keyword/g);
var clean_count = !count ? false : count.length;
Run Code Online (Sandbox Code Playgroud)
如果您有能力使用库,您可能会发现Lo-Dash JS 库非常有用。在这种情况下,请继续检查_.contains()(_.includes()从 v4 开始替换)。
(注意 Lo-Dash 约定将库对象命名为 _。\n不要忘记在同一页面中检查安装,以便为您的项目进行设置。)
\n\n_.contains("foo", "oo"); // \xe2\x86\x92 true\n_.contains("foo", "bar"); // \xe2\x86\x92 false\n// Equivalent with:\n_("foo").contains("oo"); // \xe2\x86\x92 true\n_("foo").contains("bar"); // \xe2\x86\x92 false\nRun Code Online (Sandbox Code Playgroud)\n\n对于您的情况,请继续使用:
\n\n_.contains(str, "Yes");\n// or:\n_(str).contains("Yes");\nRun Code Online (Sandbox Code Playgroud)\n\n..无论你更喜欢哪一个。
\nincludes()方法确定是否可以在另一个字符串中找到一个字符串,并根据需要返回true或false。
语法:-string.includes(searchString [,position])
searchString:-在此字符串中要搜索的字符串。
位置:-可选。该字符串中开始搜索searchString的位置;默认为0。
string = 'LOL';
console.log(string.includes('lol')); // returns false
console.log(string.includes('LOL')); // returns true
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1577726 次 |
| 最近记录: |