indexof的问题

ALbert_Yu 2006-06-19 04:38:35
indexof能不能实现想sql中的模糊查询的功能呀
...全文
158 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ALbert_Yu 2006-06-19
  • 打赏
  • 举报
回复
那我怎么还是不行呀
kevinliuu 2006-06-19
  • 打赏
  • 举报
回复
indexOf这样用没有问题的
ALbert_Yu 2006-06-19
  • 打赏
  • 举报
回复
根据你的说法我做了,我的代码如下,可是不行呀
<script>
// 查找/选择 函数
function selColor(obj){
// 定义两个字符串变量
var str1;
var str2;
// 定义列表对象
var objcolor = document.form1.a_color;
// 清除查找输入框中的空格
obj.value = clearSpace(obj.value);
// 将字符转换成小写
var objlen = obj.length;
str2 = obj.value.toLowerCase();
for (var i=0; i<objcolor.length; i++){
//比较长度
if (str2.length == objcolor[i].value.length){
// 循环查找与字符匹配的列表项
str1 = objcolor[i].text.toLowerCase();
if (str1.indexOf(str2) >=0){
// 如果找到匹配项, 选中该项
objcolor.selectedIndex = i;
// 结束函数
return;
}
}
objcolor.selectedIndex=0;
}
}
// 去空格函数
function clearSpace(str){
return str.replace(/\s+/, "");
}
</script>
<form name="form1">
<select name="a_color" size="6" id="a_color" onchange="aa.value=this.value">
<option value="0">-- 选择颜色 --</option>
<option value="LightPink" style="background-color: #FFB6C1" >浅粉(LightPink)</option>
<option value="Pink" style="background-color: #FFC0CB" >粉红(Pink)</option>
<option value="crimson" style="background-color: #DC143C" >猩红(crimson)</option>
<option value="LavenderBlush" style="background-color: #FFF0F5" >粉淡紫色(LavenderBlush)</option>
</select>
<input name="T_HELP" type="text" id="T_HELP" size="10" onKeyUp="selColor(this);">
<label>
<input name="aa" type="text" id="aa">
</label>
</form>
kevinliuu 2006-06-19
  • 打赏
  • 举报
回复
怎么不可以/?

String s = "asdfsf" ;
if(s.indexOf("fs") >= 0)
//to do here
ALbert_Yu 2006-06-19
  • 打赏
  • 举报
回复
String s = "asdfsf" ;
if(s.indexOf("fs") >= 0)
还是不可以呀
MBlue 2006-06-19
  • 打赏
  • 举报
回复
用正则表达式
kevinliuu 2006-06-19
  • 打赏
  • 举报
回复
String s = "asdfsf" ;
if(s.indexOf("fs") >= 0)
eqxu 2006-06-19
  • 打赏
  • 举报
回复


sql中有模糊查询的语句阿



like '%ql%'
kevinliuu 2006-06-19
  • 打赏
  • 举报
回复
应该是大于等于0
kevinliuu 2006-06-19
  • 打赏
  • 举报
回复
String s = "asdfsf" ;
if(s.indexOf("fs") > 0)
....


大于0就ok了

如果不包含,indexOf返回-1
ALbert_Yu 2006-06-19
  • 打赏
  • 举报
回复
就是要判断<option>中是否存在某一数据,可以是包含,用indexof做完全相等我已经实现了,可不知道包含怎么做
kevinliuu 2006-06-19
  • 打赏
  • 举报
回复
怎么讲?

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧