如何从JavaScript中输入字段删除重复项

问题描述 投票:0回答:1

$(document).ready(function(){
         $('td:even').css('background-color', 'limegreen');
		 $('td:odd').css('background-color', 'blue');
		  
		//iterate through each textboxes and add keyup
		//handler to trigger sum event
		$(".txt").each(function() {
			 
			$(this).change(function(){
				
				calculateSum();
				checkduplicate();
				
				
			});
		});

	});
	function calculateSum() {
		var sum = 0;
		//iterate through each textboxes and add the values
		$(".txt").each(function() {
			//add only if the value is number
			if(!isNaN(this.value) && this.value.length!=0) {
				sum += parseFloat(this.value);
			}
		});
		//.toFixed() method will roundoff the final sum to 2 decimal places
		$("#sum").html(sum.toFixed(2));
		
	}

    function checkduplicate(){
	var sum = 0;
  var arr = [];
  var all = document.querySelectorAll('input[type=txt]');
  all.forEach(function(el){
    arr.push(el.value);
    sum += +el.value;
console.log(arr);
console.log(sum);
    var c = arr.filter(i =>i==el.value).length;
    if(c>1){
      var dupe = [...all].filter(e => e.value==el.value);
      dupe.forEach(function(d){
        if(d.value.trim() != ''){
          d.style.border = '5px solid red';
          d.style.color = 'red';
          document.body.style.backgroundColor = "red";
        }
      });
    }
    });
  	}		
	$(document).ready(function(){
		$('#btnClear').click(function(){				
			if(confirm("Want to clear?")){

				/*Clear all input type="text" box*/
				$('#sumtable input[type="txt"]').val(" ");
				/*Reset sum to zero*/
			     $("span").html("0");
				 document.body.style.backgroundColor = "white";
				 location.reload();
	             $('td:even').css('background-color', 'limegreen');
		        $('td:odd').css('background-color', 'blue');
				$(document).ready(function(){
         $('td:even').css('background-color', 'limegreen');
		 $('td:odd').css('background-color', 'blue');
		//iterate through each textboxes and add keyup
		//handler to trigger sum event
		$(".txt").each(function() {

			$(this).change(function(){
				calculateSum();
				checkduplicate();
				 location.reload();
	             $('td:even').css('background-color', 'limegreen');
		        $('td:odd').css('background-color', 'blue');
				/*Clear all input type="text" box*/
				$('#sumtable input[type="txt"]').val(" ");
				/*Reset sum to zero*/
			     $("span").html("0");
				 document.body.style.backgroundColor = "white";
				
			});
		});

	});
				
			}					
		});
	});
body {
	
				font-family: sans-serif;
			}
			#summation {
				font-size: 18px;
				font-weight: bold;
				color:#174C68;
			}
			.txt {
				background-color: #FEFFB0;
				font-weight: bold;
				text-align: right;
				
			}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<meta charset="utf-8"/>
<head>
<!--<script src="jquery-3.3.1.min.js"></script>-->
<script src="/home/cybrosys/Sethu/JS/jquery-3.3.1.min.js"></script>
<script src="sumbox.js"></script>
 <link rel="stylesheet" href="sumbox.css" type="txt/css">
</head>
<body>
<table  border="2" align="center" id="sumtable">
 <tbody>
        <tr>
          <td><input type="txt" class="txt"   name="txt"    id="qty1" class="form-control alldifferent"></td>
          <td><input type="txt" class="txt"   name="txt"    id="qty2" class="form-control alldifferent"></td>
          <td><input type="txt"class="txt"    name="txt"    id="qty3" class="form-control alldifferent" ></td>
        </tr>
        <tr>
          <td><input type="txt" class="txt"     name="txt"  id="qty4" class="form-control alldifferent" ></td>
          <td><input type="txt" class="txt"   name="txt"    id="qty5"  class="form-control alldifferent"></td>
          <td><input type="txt" class="txt"   name="txt"    id="qty6" class="form-control alldifferent" ></td>
        </tr>
        <tr>
          <td><input type="txt" class="txt"   name="txt"    id="qty7" class="form-control alldifferent" ></td>
          <td><input type="txt"class="txt"     name="txt"   id="qty8"  class="form-control alldifferent"></td>
          <td><input type="txt" class="txt"    name="txt"   id="qty9"  class="form-control alldifferent"></td>
        </tr>
        </tbody>
		</table>
<div align="center" >
	<tr id="summation"><br>
		<td align="center">Sum :</td>
		<td align="center"><span id="sum">0</span></td></tr>
		</div>
		<td>&nbsp;</td>
		<div align="center">
         <tr><td><button type="button" id="btnClear" value="reset"/>Reset</td></tr>
</div>
	</tr>
</body>
</html>

我无法从这些输入字段输入字段和自动对焦删除重复项。此外,当我点击复位按钮产生的总和是楠。

我有9个输入框,用户可以输入值。当输入值产生的总和。当用户输入重复值的那些条目突出显示与红色颜色和背景变红太。

我试图删除这些领域的重复条目和自动对焦。重置总和为零,输入字段后为null。而再次输入值产生的总和是NaN。如何解决这两个问题?

javascript
1个回答
0
投票

你需要,当你做的总和来改变颜色,并添加d.focus();。尝试这个:

$(document).ready(function(){
         $('td:even').css('background-color', 'limegreen');
		 $('td:odd').css('background-color', 'blue');
		  
		//iterate through each textboxes and add keyup
		//handler to trigger sum event
		$(".txt").each(function() {
			 
			$(this).change(function(){
				
				calculateSum();
				checkduplicate();
				
				
			});
		});

	});
var sum = 0;
	function calculateSum() {
		sum = 0;
		//iterate through each textboxes and add the values
		$(".txt").each(function() {
			//add only if the value is number
			if(!isNaN(this.value) && this.value.length!=0) {
				sum += parseFloat(this.value);
 this.style.color ='limegreen';
        $('td:even').css('background-color', 'limegreen');
		        $('td:odd').css('background-color', 'blue');
            this.style.border = 'limegreen';
			}
		});
		//.toFixed() method will roundoff the final sum to 2 decimal places
		$("#sum").html(sum.toFixed(2));
		
	}

    function checkduplicate(){
	var check=0;
  var arr = [];
  var all = document.querySelectorAll('input[type=txt]');
  all.forEach(function(el){
    arr.push(el.value);
    sum += +el.value;
   // console.log(arr);
//console.log(sum);
    var c = arr.filter(i =>i==el.value).length;
    if(c>1){
      var dupe = [...all].filter(e => e.value==el.value);
       check=0;
      dupe.forEach(function(d){
        if(d.value.trim() != ''){
        if (check==0){
        check=1;
        }else{
          d.style.border = '5px solid red';
          d.style.color = 'red';
          d.value="";
          d.focus();}
       
          document.body.style.backgroundColor = "red";
        }
else{
 d.style.color ='limegreen';
 $('td:even').css('background-color', 'limegreen');
		        $('td:odd').css('background-color', 'blue');
  document.body.style.backgroundColor = "white";
}
      });
    }
    });
  	}		
	$(document).ready(function(){
		$('#btnClear').click(function(){				
			if(confirm("Want to clear?")){

				/*Clear all input type="text" box*/
				$('#sumtable input[type="txt"]').val(" ");
				/*Reset sum to zero*/
			     $("span").html("0");
				 document.body.style.backgroundColor = "white";
				setTimeout(function(){
  window.location.reload(true);
});
	             $('td:even').css('background-color', 'limegreen');
		        $('td:odd').css('background-color', 'blue');
				$(document).ready(function(){
         $('td:even').css('background-color', 'limegreen');
		 $('td:odd').css('background-color', 'blue');
		//iterate through each textboxes and add keyup
		//handler to trigger sum event
		$(".txt").each(function() {

			$(this).change(function(){
				calculateSum();
				checkduplicate();
							setTimeout(function(){
  window.location.reload(true);
});
	             $('td:even').css('background-color', 'limegreen');
		        $('td:odd').css('background-color', 'blue');
				/*Clear all input type="text" box*/
				$('#sumtable input[type="txt"]').val(" ");
				/*Reset sum to zero*/
			     $("span").html("0");
				 document.body.style.backgroundColor = "white";
				
			});
		});

	});
				
			}					
		});
	});
body {
	
				font-family: sans-serif;
			}
			#summation {
				font-size: 18px;
				font-weight: bold;
				color:#174C68;
			}
			.txt {
				background-color: #FEFFB0;
				font-weight: bold;
				text-align: right;
				
			}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<meta charset="utf-8"/>
<head>
<!--<script src="jquery-3.3.1.min.js"></script>-->
<script src="/home/cybrosys/Sethu/JS/jquery-3.3.1.min.js"></script>
<script src="sumbox.js"></script>
 <link rel="stylesheet" href="sumbox.css" type="txt/css">
</head>
<body>
<table  border="2" align="center" id="sumtable">
 <tbody>
        <tr>
          <td><input type="txt" class="txt"   name="txt"    id="qty1" class="form-control alldifferent"></td>
          <td><input type="txt" class="txt"   name="txt"    id="qty2" class="form-control alldifferent"></td>
          <td><input type="txt"class="txt"    name="txt"    id="qty3" class="form-control alldifferent" ></td>
        </tr>
        <tr>
          <td><input type="txt" class="txt"     name="txt"  id="qty4" class="form-control alldifferent" ></td>
          <td><input type="txt" class="txt"   name="txt"    id="qty5"  class="form-control alldifferent"></td>
          <td><input type="txt" class="txt"   name="txt"    id="qty6" class="form-control alldifferent" ></td>
        </tr>
        <tr>
          <td><input type="txt" class="txt"   name="txt"    id="qty7" class="form-control alldifferent" ></td>
          <td><input type="txt"class="txt"     name="txt"   id="qty8"  class="form-control alldifferent"></td>
          <td><input type="txt" class="txt"    name="txt"   id="qty9"  class="form-control alldifferent"></td>
        </tr>
        </tbody>
		</table>
<div align="center" >
	<tr id="summation"><br>
		<td align="center">Sum :</td>
		<td align="center"><span id="sum">0</span></td></tr>
		</div>
		<td>&nbsp;</td>
		<div align="center">
         <tr><td><button type="button" id="btnClear" value="reset"/>Reset</td></tr>
</div>
	</tr>
</body>
</html>

对于Firefox使用location.reload(true);

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.