js 如何快速判断文本筐内容重复 我现在有1000个文本域如何快速判断文本内容有没有重复,并提示。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼主这样问题有些宽泛,假设有相同的元素a那么每个文本域用spit()函数来计算一下,或者indexOf就可以判断了, 但问题是重复的元素不知道是什么,那就难办了,几乎就无法比较了 1,不要用正则,没必要,也慢2,使用一个数组,存下每个input的value3,使用循环,每经过一个input,就把它的value对比一次基本上,不会有很高效的办法,除非是纯英文,还可以用索引,分出a-z的子数组var arrValues = [];function checkExisted(value){ var len = arrValues.length; for(var i = 0; i < len; i++){ if(arrValues[i] == value){ return true; } } arrValues[len] = value; return false;}/** *这1000个input的命名肯定是有规范的吧,比如,以input_打头 */function loopInputs(){ for(var i = 0; i < 1000; i++){ var input = document.getElementById('input_' + i); return checkExisted(input.value); } return false;} <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head> <script type="text/javascript"> window.onload=function(){ //我现在有1000个文本域如何快速判断文本内容有没有重复,并提示。 for(var i=0;i<10;i++){ var obj=document.createElement("input"); obj.type="text"; document.body.appendChild(obj); } } function check(){ var value=document.body.innerHTML; if(/(value=[^>]+>).*?\1/g.test(value))alert("repeat"); } </script></head><body><input type=button onclick="check();"></body></html> function(){var temp = {};var inputs = document.getElementsByTagName("input");if(inputs.length > 0){ for(var i=0;i<inputs.length;i++){ var value = inputs[0].value; if(!temp[value]){ temp[value] = true; }else{ return true; } } return false;}} [code=HTML]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title><script type="text/javascript">window.onload=function test() {for(var i=0;i<1000;i++){ addRow(i); } }function check(obj){ if(obj.value.length==0) return ; var arrText=document.getElementsByTagName("input"); for(var i=0;i<arrText.length;i++ ) { if(arrText.item(i).type=="text") { if(obj.value==arrText.item(i).value&&obj.id!=arrText.item(i).id) { alert("values is complux"); obj.value=""; return; } } }}function addRow(num){ var newTr=testTbl.insertRow(); var newTd0 = newTr.insertCell(); newTd0.innerHTML = '<input type=text onblur="check(this)" id="txt'+num+'">'; }</script></head><body><table id="testTbl" border=1></table></body></html>的确挺慢的,我觉得能提高的话也就是 在判断条件上作些文章,比方说空的直接不考虑,减少一些判断等等。我在提供个思路:能不能把这一千个文本域(所有)的值合并成一个字符串,然后再利用正则或者其他的index等进行查找呢 没时间做了,在上班呢。~[/code] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head> <script type="text/javascript"> var arr=[]; window.onload=function(){ //我现在有1000个文本域如何快速判断文本内容有没有重复,并提示。 for(var i=0;i<10;i++){ var obj=document.createElement("input"); obj.type="text"; obj.id="txt"+i; obj.onchange=function(){ arr[this.id.substring(3)]=this.value; } document.body.appendChild(obj); } } function check(){ var value=arr.join(":"); if(/([^:]+).*?\1/g.test(value))alert("repeat"); alert(value); } </script></head><body><input type=button onclick="check();"></body></html> 訂正:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head> <script type="text/javascript"> var arr=[]; window.onload=function(){ //我现在有1000个文本域如何快速判断文本内容有没有重复,并提示。 for(var i=0;i<10;i++){ var obj=document.createElement("input"); obj.type="text"; obj.id="txt"+i; obj.onchange=function(){ arr[this.id.substring(3)]=this.value; } document.body.appendChild(obj); } } function check(){ var value=":"+arr.join("::")+":"; if(/(:[^:]+:).*?\1/g.test(value))alert("repeat"); alert(value); } </script></head><body><input type=button onclick="check();"></body></html> zTree设置的自定义target为什么不起作用 哪位大神能帮我写一下代码 通过css手段,使iframe的宽高度自适应,在线等 IE6有问题,FF正常 什么原因 急,请问javascript的this对象问题? 为什么我的alert没任何反应而alert2能执行alert("a")!在线等 javascript 小数运算 12.2986+9.0966 = ? 项目开发问题,请大家帮助,谢谢! 怎么控制层在800*600和1024*768下保持一样的位置 那位大神会做 ie8和ie6在解释脚本时的差别 各位高手,请问下I6的指针问题.
那么每个文本域用spit()函数来计算一下,或者indexOf就可以判断了, 但问题是重复的元素不知道是什么,那就难办了,几乎就无法比较了
2,使用一个数组,存下每个input的value
3,使用循环,每经过一个input,就把它的value对比一次
基本上,不会有很高效的办法,除非是纯英文,还可以用索引,分出a-z的子数组
var arrValues = [];function checkExisted(value){
var len = arrValues.length;
for(var i = 0; i < len; i++){
if(arrValues[i] == value){
return true;
}
}
arrValues[len] = value;
return false;
}/**
*这1000个input的命名肯定是有规范的吧,比如,以input_打头
*/
function loopInputs(){
for(var i = 0; i < 1000; i++){
var input = document.getElementById('input_' + i);
return checkExisted(input.value);
}
return false;
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
window.onload=function(){
//我现在有1000个文本域如何快速判断文本内容有没有重复,并提示。
for(var i=0;i<10;i++){
var obj=document.createElement("input");
obj.type="text";
document.body.appendChild(obj);
}
}
function check(){
var value=document.body.innerHTML;
if(/(value=[^>]+>).*?\1/g.test(value))alert("repeat");
}
</script>
</head>
<body>
<input type=button onclick="check();">
</body>
</html>
var temp = {};var inputs = document.getElementsByTagName("input");
if(inputs.length > 0){
for(var i=0;i<inputs.length;i++){
var value = inputs[0].value;
if(!temp[value]){
temp[value] = true;
}else{
return true;
}
}
return false;
}
}
[code=HTML]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
window.onload=function test()
{
for(var i=0;i<1000;i++)
{
addRow(i);
}
}
function check(obj)
{
if(obj.value.length==0)
return ;
var arrText=document.getElementsByTagName("input");
for(var i=0;i<arrText.length;i++ )
{
if(arrText.item(i).type=="text")
{
if(obj.value==arrText.item(i).value&&obj.id!=arrText.item(i).id)
{
alert("values is complux");
obj.value="";
return;
}
}
}
}
function addRow(num)
{
var newTr=testTbl.insertRow();
var newTd0 = newTr.insertCell();
newTd0.innerHTML = '<input type=text onblur="check(this)" id="txt'+num+'">';
}
</script>
</head>
<body>
<table id="testTbl" border=1>
</table>
</body>
</html>的确挺慢的,我觉得能提高的话也就是 在判断条件上作些文章,比方说空的直接不考虑,减少一些判断等等。我在提供个思路:能不能把这一千个文本域(所有)的值合并成一个字符串,然后再利用正则或者其他的index等进行查找呢 没时间做了,在上班呢。~[/code]
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
var arr=[];
window.onload=function(){
//我现在有1000个文本域如何快速判断文本内容有没有重复,并提示。
for(var i=0;i<10;i++){
var obj=document.createElement("input");
obj.type="text";
obj.id="txt"+i;
obj.onchange=function(){
arr[this.id.substring(3)]=this.value;
}
document.body.appendChild(obj);
}
}
function check(){
var value=arr.join(":");
if(/([^:]+).*?\1/g.test(value))alert("repeat");
alert(value);
}
</script>
</head>
<body>
<input type=button onclick="check();">
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
var arr=[];
window.onload=function(){
//我现在有1000个文本域如何快速判断文本内容有没有重复,并提示。
for(var i=0;i<10;i++){
var obj=document.createElement("input");
obj.type="text";
obj.id="txt"+i;
obj.onchange=function(){
arr[this.id.substring(3)]=this.value;
}
document.body.appendChild(obj);
}
}
function check(){
var value=":"+arr.join("::")+":";
if(/(:[^:]+:).*?\1/g.test(value))alert("repeat");
alert(value);
}
</script>
</head>
<body>
<input type=button onclick="check();">
</body>
</html>