How to Change Ifs to For

Asked

Viewed 124 times

0

I have 3 dropdown’s with equal values (0.1, 2 and 3). Where 0 is 'Select a collaborator' 1 is 'Fábia' 2 is 'Helio' 3 is 'Sueli' I have to select a value on one dropdown and disappear from the others. Example: I select Sueli in the first Dropdown and has to disappear from the Second and Third, but in this second and third Fabio and Helio have to appear in the Second and Fabia and Helio in the Third. And when I select one of those names in the Second and Third you have to shape the 3 dropdowns according to that rule. One thing that also has to happen is when I select the item (value 0, 'Select a collaborator'), you have to go back to all the other dropdowns. I managed to do with Ifs but ta giant. The point is, I don’t know how to do For. I wanted a light. A help to learn. If anyone could help me, I’d really appreciate it. I did so:

function fnFuncao() {

	
	var varField_idFacilitador1 = $("#varField_idFacilitador1 option:selected").val();	
	var varField_idFacilitador2 = $("#varField_idFacilitador2 option:selected").val();
	var varField_idFacilitador3 = $("#varField_idFacilitador3 option:selected").val();

	if (parseInt(varField_idFacilitador1) == parseInt(0)){
	
		$("#varField_idFacilitador1 option[value='0']").show();	
		$("#varField_idFacilitador1 option[value='1']").show();
		$("#varField_idFacilitador2 option[value='1']").show();	
		$("#varField_idFacilitador3 option[value='1']").show();	
		
	}	
	
	if (parseInt(varField_idFacilitador2) == parseInt(0)){

		$("#varField_idFacilitador2 option[value='0']").show();	
		$("#varField_idFacilitador1 option[value='2']").show();
		$("#varField_idFacilitador2 option[value='2']").show();	
		$("#varField_idFacilitador3 option[value='2']").show();	
		
	}

	if (parseInt(varField_idFacilitador3) == parseInt(0)){

		$("#varField_idFacilitador3 option[value='0']").show();		
		$("#varField_idFacilitador1 option[value='3']").show();
		$("#varField_idFacilitador2 option[value='3']").show();	
		$("#varField_idFacilitador3 option[value='3']").show();	
		
	}		
	
	if (parseInt(varField_idFacilitador1) == parseInt(1)){

		$("#varField_idFacilitador1 option[value='0']").show();
		$("#varField_idFacilitador1 option[value="+varField_idFacilitador1+"]").show();
		$("#varField_idFacilitador2 option[value="+varField_idFacilitador1+"]").hide();	
		$("#varField_idFacilitador3 option[value="+varField_idFacilitador1+"]").hide();	
		
	}
	if(parseInt(varField_idFacilitador1) == parseInt(2)){
		$("#varField_idFacilitador1 option[value='0']").show();	
		$("#varField_idFacilitador1 option[value="+varField_idFacilitador1+"]").show();	
		$("#varField_idFacilitador2 option[value="+varField_idFacilitador1+"]").hide();	
		$("#varField_idFacilitador3 option[value="+varField_idFacilitador1+"]").hide();	
	}
	if(parseInt(varField_idFacilitador1) == parseInt(3)){

		$("#varField_idFacilitador1 option[value='0']").show();		
		$("#varField_idFacilitador1 option[value="+varField_idFacilitador1+"]").show();		
		$("#varField_idFacilitador2 option[value="+varField_idFacilitador1+"]").hide();	
		$("#varField_idFacilitador3 option[value="+varField_idFacilitador1+"]").hide();	
	}
	
	

	if 	(parseInt(varField_idFacilitador2) == parseInt(1)){	
	
		$("#varField_idFacilitador2 option[value='0']").show();		
		$("#varField_idFacilitador2 option[value="+varField_idFacilitador2+"]").show();	
		$("#varField_idFacilitador1 option[value="+varField_idFacilitador2+"]").hide();	
		$("#varField_idFacilitador3 option[value="+varField_idFacilitador2+"]").hide();	
		
	}
	if(parseInt(varField_idFacilitador2) == parseInt(2)){
	
		$("#varField_idFacilitador2 option[value='0']").show();		
		$("#varField_idFacilitador2 option[value="+varField_idFacilitador2+"]").show();		
		$("#varField_idFacilitador1 option[value="+varField_idFacilitador2+"]").hide();	
		$("#varField_idFacilitador3 option[value="+varField_idFacilitador2+"]").hide();	
	}
	if(parseInt(varField_idFacilitador2) == parseInt(3)){
	
		$("#varField_idFacilitador2 option[value='0']").show();		
		$("#varField_idFacilitador2 option[value="+varField_idFacilitador2+"]").show();		
		$("#varField_idFacilitador1 option[value="+varField_idFacilitador2+"]").hide();	
		$("#varField_idFacilitador3 option[value="+varField_idFacilitador2+"]").hide();	
	}	
	
	
	if 	(parseInt(varField_idFacilitador3) == parseInt(1)){
	
		$("#varField_idFacilitador3 option[value='0']").show();		
		$("#varField_idFacilitador3 option[value="+varField_idFacilitador3+"]").show();		
		$("#varField_idFacilitador1 option[value="+varField_idFacilitador3+"]").hide();	
		$("#varField_idFacilitador2 option[value="+varField_idFacilitador3+"]").hide();	
		
	}
	if(parseInt(varField_idFacilitador3) == parseInt(2)){
	
		$("#varField_idFacilitador3 option[value='0']").show();			
		$("#varField_idFacilitador3 option[value="+varField_idFacilitador3+"]").show();		
		$("#varField_idFacilitador1 option[value="+varField_idFacilitador3+"]").hide();	
		$("#varField_idFacilitador2 option[value="+varField_idFacilitador3+"]").hide();	
	}
	if(parseInt(varField_idFacilitador3) == parseInt(3)){
	
		$("#varField_idFacilitador3 option[value='0']").show();			
		$("#varField_idFacilitador3 option[value="+varField_idFacilitador3+"]").show();		
		$("#varField_idFacilitador1 option[value="+varField_idFacilitador3+"]").hide();	
		$("#varField_idFacilitador2 option[value="+varField_idFacilitador3+"]").hide();	
	}		
	
	if (parseInt(varField_idFacilitador1) == parseInt(0) && parseInt(varField_idFacilitador2) == parseInt(0) && parseInt(varField_idFacilitador3) == parseInt(0)){
		$("#varField_idFacilitador1 option[value='0']").show();
		$("#varField_idFacilitador2 option[value='0']").show();	
		$("#varField_idFacilitador3 option[value='0']").show();			
		$("#varField_idFacilitador1 option[value='1']").show();
		$("#varField_idFacilitador2 option[value='1']").show();	
		$("#varField_idFacilitador3 option[value='1']").show();		
		$("#varField_idFacilitador1 option[value='2']").show();
		$("#varField_idFacilitador2 option[value='2']").show();	
		$("#varField_idFacilitador3 option[value='2']").show();
		$("#varField_idFacilitador1 option[value='3']").show();
		$("#varField_idFacilitador2 option[value='3']").show();	
		$("#varField_idFacilitador3 option[value='3']").show();		
	}

	
	if (parseInt(varField_idFacilitador1) == parseInt(1) && parseInt(varField_idFacilitador2) == parseInt(0) && parseInt(varField_idFacilitador3) == parseInt(0)){
		$("#varField_idFacilitador1 option[value='1']").show();
		$("#varField_idFacilitador2 option[value='1']").hide();	
		$("#varField_idFacilitador3 option[value='1']").hide();		
		$("#varField_idFacilitador1 option[value='2']").show();
		$("#varField_idFacilitador2 option[value='2']").show();	
		$("#varField_idFacilitador3 option[value='2']").show();
		$("#varField_idFacilitador1 option[value='3']").show();
		$("#varField_idFacilitador2 option[value='3']").show();	
		$("#varField_idFacilitador3 option[value='3']").show();		
	}
	
	if (parseInt(varField_idFacilitador1) == parseInt(2) && parseInt(varField_idFacilitador2) == parseInt(0) && parseInt(varField_idFacilitador3) == parseInt(0)){
		$("#varField_idFacilitador1 option[value='1']").show();
		$("#varField_idFacilitador2 option[value='1']").show();	
		$("#varField_idFacilitador3 option[value='1']").show();		
		$("#varField_idFacilitador1 option[value='2']").show();
		$("#varField_idFacilitador2 option[value='2']").hide();	
		$("#varField_idFacilitador3 option[value='2']").hide();
		$("#varField_idFacilitador1 option[value='3']").show();
		$("#varField_idFacilitador2 option[value='3']").show();	
		$("#varField_idFacilitador3 option[value='3']").show();		
	}

	if (parseInt(varField_idFacilitador1) == parseInt(3) && parseInt(varField_idFacilitador2) == parseInt(0) && parseInt(varField_idFacilitador3) == parseInt(0)){
		$("#varField_idFacilitador1 option[value='1']").show();
		$("#varField_idFacilitador2 option[value='1']").show();	
		$("#varField_idFacilitador3 option[value='1']").show();		
		$("#varField_idFacilitador1 option[value='2']").show();
		$("#varField_idFacilitador2 option[value='2']").show();	
		$("#varField_idFacilitador3 option[value='2']").show();
		$("#varField_idFacilitador1 option[value='3']").show();
		$("#varField_idFacilitador2 option[value='3']").hide();	
		$("#varField_idFacilitador3 option[value='3']").hide();		
	}	
	
	if (parseInt(varField_idFacilitador1) == parseInt(0) && parseInt(varField_idFacilitador2) == parseInt(1) && parseInt(varField_idFacilitador3) == parseInt(0)){
		$("#varField_idFacilitador1 option[value='1']").hide();
		$("#varField_idFacilitador2 option[value='1']").show();	
		$("#varField_idFacilitador3 option[value='1']").hide();		
		$("#varField_idFacilitador1 option[value='2']").show();
		$("#varField_idFacilitador2 option[value='2']").show();	
		$("#varField_idFacilitador3 option[value='2']").show();
		$("#varField_idFacilitador1 option[value='3']").show();
		$("#varField_idFacilitador2 option[value='3']").show();	
		$("#varField_idFacilitador3 option[value='3']").show();		
	}
	
	if (parseInt(varField_idFacilitador1) == parseInt(0) && parseInt(varField_idFacilitador2) == parseInt(2) && parseInt(varField_idFacilitador3) == parseInt(0)){
		$("#varField_idFacilitador1 option[value='1']").show();
		$("#varField_idFacilitador2 option[value='1']").show();	
		$("#varField_idFacilitador3 option[value='1']").show();		
		$("#varField_idFacilitador1 option[value='2']").hide();
		$("#varField_idFacilitador2 option[value='2']").show();	
		$("#varField_idFacilitador3 option[value='2']").hide();
		$("#varField_idFacilitador1 option[value='3']").show();
		$("#varField_idFacilitador2 option[value='3']").show();	
		$("#varField_idFacilitador3 option[value='3']").show();		
	}

	if (parseInt(varField_idFacilitador1) == parseInt(0) && parseInt(varField_idFacilitador2) == parseInt(3) && parseInt(varField_idFacilitador3) == parseInt(0)){
		$("#varField_idFacilitador1 option[value='1']").show();
		$("#varField_idFacilitador2 option[value='1']").show();	
		$("#varField_idFacilitador3 option[value='1']").show();		
		$("#varField_idFacilitador1 option[value='2']").show();
		$("#varField_idFacilitador2 option[value='2']").show();	
		$("#varField_idFacilitador3 option[value='2']").show();
		$("#varField_idFacilitador1 option[value='3']").hide();
		$("#varField_idFacilitador2 option[value='3']").show();	
		$("#varField_idFacilitador3 option[value='3']").hide();		
	}		
	
	if (parseInt(varField_idFacilitador1) == parseInt(0) && parseInt(varField_idFacilitador2) == parseInt(0) && parseInt(varField_idFacilitador3) == parseInt(1)){
		$("#varField_idFacilitador1 option[value='1']").hide();
		$("#varField_idFacilitador2 option[value='1']").hide();	
		$("#varField_idFacilitador3 option[value='1']").show();		
		$("#varField_idFacilitador1 option[value='2']").show();
		$("#varField_idFacilitador2 option[value='2']").show();	
		$("#varField_idFacilitador3 option[value='2']").show();
		$("#varField_idFacilitador1 option[value='3']").show();
		$("#varField_idFacilitador2 option[value='3']").show();	
		$("#varField_idFacilitador3 option[value='3']").show();		
	}
	
	if (parseInt(varField_idFacilitador1) == parseInt(0) && parseInt(varField_idFacilitador2) == parseInt(0) && parseInt(varField_idFacilitador3) == parseInt(2)){
		$("#varField_idFacilitador1 option[value='1']").show();
		$("#varField_idFacilitador2 option[value='1']").show();	
		$("#varField_idFacilitador3 option[value='1']").show();		
		$("#varField_idFacilitador1 option[value='2']").hide();
		$("#varField_idFacilitador2 option[value='2']").hide();	
		$("#varField_idFacilitador3 option[value='2']").show();
		$("#varField_idFacilitador1 option[value='3']").show();
		$("#varField_idFacilitador2 option[value='3']").show();	
		$("#varField_idFacilitador3 option[value='3']").show();		
	}

	if (parseInt(varField_idFacilitador1) == parseInt(0) && parseInt(varField_idFacilitador2) == parseInt(0) && parseInt(varField_idFacilitador3) == parseInt(3)){
		$("#varField_idFacilitador1 option[value='1']").show();
		$("#varField_idFacilitador2 option[value='1']").show();	
		$("#varField_idFacilitador3 option[value='1']").show();		
		$("#varField_idFacilitador1 option[value='2']").show();
		$("#varField_idFacilitador2 option[value='2']").show();	
		$("#varField_idFacilitador3 option[value='2']").show();
		$("#varField_idFacilitador1 option[value='3']").hide();
		$("#varField_idFacilitador2 option[value='3']").hide();	
		$("#varField_idFacilitador3 option[value='3']").show();		
	}		
	
	if (parseInt(varField_idFacilitador1) == parseInt(1) && parseInt(varField_idFacilitador2) == parseInt(0) && parseInt(varField_idFacilitador3) == parseInt(2)){
		$("#varField_idFacilitador1 option[value='1']").show();
		$("#varField_idFacilitador1 option[value='2']").hide();
		$("#varField_idFacilitador1 option[value='3']").show();
		
		$("#varField_idFacilitador2 option[value='1']").hide();	
		$("#varField_idFacilitador2 option[value='2']").hide();	
		$("#varField_idFacilitador2 option[value='3']").show();	
		
		$("#varField_idFacilitador3 option[value='1']").hide();		
		$("#varField_idFacilitador3 option[value='2']").show();
		$("#varField_idFacilitador3 option[value='3']").show();		
	}	
	
	if (parseInt(varField_idFacilitador1) == parseInt(1) && parseInt(varField_idFacilitador2) == parseInt(0) && parseInt(varField_idFacilitador3) == parseInt(2)){
		$("#varField_idFacilitador1 option[value='1']").show();
		$("#varField_idFacilitador1 option[value='2']").hide();
		$("#varField_idFacilitador1 option[value='3']").show();
		
		$("#varField_idFacilitador2 option[value='1']").hide();	
		$("#varField_idFacilitador2 option[value='2']").hide();	
		$("#varField_idFacilitador2 option[value='3']").show();	
		
		$("#varField_idFacilitador3 option[value='1']").hide();		
		$("#varField_idFacilitador3 option[value='2']").show();
		$("#varField_idFacilitador3 option[value='3']").show();		
	}	

	if (parseInt(varField_idFacilitador1) == parseInt(2) && parseInt(varField_idFacilitador2) == parseInt(3) && parseInt(varField_idFacilitador3) == parseInt(0)){
		$("#varField_idFacilitador1 option[value='1']").show();
		$("#varField_idFacilitador1 option[value='2']").show();
		$("#varField_idFacilitador1 option[value='3']").hide();
		
		$("#varField_idFacilitador2 option[value='1']").show();	
		$("#varField_idFacilitador2 option[value='2']").hide();	
		$("#varField_idFacilitador2 option[value='3']").show();	
		
		$("#varField_idFacilitador3 option[value='1']").show();		
		$("#varField_idFacilitador3 option[value='2']").hide();
		$("#varField_idFacilitador3 option[value='3']").hide();		
	}	
	
	
	if (parseInt(varField_idFacilitador1) == parseInt(1) && parseInt(varField_idFacilitador2) == parseInt(3) && parseInt(varField_idFacilitador3) == parseInt(0)){
		$("#varField_idFacilitador1 option[value='1']").show();
		$("#varField_idFacilitador1 option[value='2']").show();
		$("#varField_idFacilitador1 option[value='3']").hide();
		
		$("#varField_idFacilitador2 option[value='1']").hide();	
		$("#varField_idFacilitador2 option[value='2']").show();	
		$("#varField_idFacilitador2 option[value='3']").show();	
		
		$("#varField_idFacilitador3 option[value='1']").hide();		
		$("#varField_idFacilitador3 option[value='2']").show();
		$("#varField_idFacilitador3 option[value='3']").hide();		
	}	
	
	if (parseInt(varField_idFacilitador1) == parseInt(2) && parseInt(varField_idFacilitador2) == parseInt(0) && parseInt(varField_idFacilitador3) == parseInt(1)){
		$("#varField_idFacilitador1 option[value='1']").hide();
		$("#varField_idFacilitador1 option[value='2']").show();
		$("#varField_idFacilitador1 option[value='3']").show();
		
		$("#varField_idFacilitador2 option[value='1']").hide();	
		$("#varField_idFacilitador2 option[value='2']").hide();	
		$("#varField_idFacilitador2 option[value='3']").show();	
		
		$("#varField_idFacilitador3 option[value='1']").show();		
		$("#varField_idFacilitador3 option[value='2']").hide();
		$("#varField_idFacilitador3 option[value='3']").show();		
	}		
	
	if (parseInt(varField_idFacilitador1) == parseInt(0) && parseInt(varField_idFacilitador2) == parseInt(3) && parseInt(varField_idFacilitador3) == parseInt(1)){
		$("#varField_idFacilitador1 option[value='1']").hide();
		$("#varField_idFacilitador1 option[value='2']").show();
		$("#varField_idFacilitador1 option[value='3']").hide();
		
		$("#varField_idFacilitador2 option[value='1']").hide();	
		$("#varField_idFacilitador2 option[value='2']").show();	
		$("#varField_idFacilitador2 option[value='3']").show();	
		
		$("#varField_idFacilitador3 option[value='1']").show();		
		$("#varField_idFacilitador3 option[value='2']").show();
		$("#varField_idFacilitador3 option[value='3']").hide();		
	}	

		if (parseInt(varField_idFacilitador1) == parseInt(0) && parseInt(varField_idFacilitador2) == parseInt(1) && parseInt(varField_idFacilitador3) == parseInt(3)){
		$("#varField_idFacilitador1 option[value='1']").hide();
		$("#varField_idFacilitador1 option[value='2']").show();
		$("#varField_idFacilitador1 option[value='3']").hide();
		
		$("#varField_idFacilitador2 option[value='1']").show();	
		$("#varField_idFacilitador2 option[value='2']").show();	
		$("#varField_idFacilitador2 option[value='3']").hide();	
		
		$("#varField_idFacilitador3 option[value='1']").hide();		
		$("#varField_idFacilitador3 option[value='2']").show();
		$("#varField_idFacilitador3 option[value='3']").show();		
	}	
	
		if (parseInt(varField_idFacilitador1) == parseInt(0) && parseInt(varField_idFacilitador2) == parseInt(1) && parseInt(varField_idFacilitador3) == parseInt(2)){
		$("#varField_idFacilitador1 option[value='1']").hide();
		$("#varField_idFacilitador1 option[value='2']").hide();
		$("#varField_idFacilitador1 option[value='3']").show();
		
		$("#varField_idFacilitador2 option[value='1']").show();	
		$("#varField_idFacilitador2 option[value='2']").hide();	
		$("#varField_idFacilitador2 option[value='3']").show();	
		
		$("#varField_idFacilitador3 option[value='1']").hide();		
		$("#varField_idFacilitador3 option[value='2']").show();
		$("#varField_idFacilitador3 option[value='3']").show();		
	}		
 		
		if (parseInt(varField_idFacilitador1) == parseInt(3) && parseInt(varField_idFacilitador2) == parseInt(2) && parseInt(varField_idFacilitador3) == parseInt(0)){
		$("#varField_idFacilitador1 option[value='1']").show();
		$("#varField_idFacilitador1 option[value='2']").hide();
		$("#varField_idFacilitador1 option[value='3']").show();
		
		$("#varField_idFacilitador2 option[value='1']").show();	
		$("#varField_idFacilitador2 option[value='2']").show();	
		$("#varField_idFacilitador2 option[value='3']").hide();	
		
		$("#varField_idFacilitador3 option[value='1']").show();		
		$("#varField_idFacilitador3 option[value='2']").hide();
		$("#varField_idFacilitador3 option[value='3']").hide();		
	}	
	
		if (parseInt(varField_idFacilitador1) == parseInt(3) && parseInt(varField_idFacilitador2) == parseInt(1) && parseInt(varField_idFacilitador3) == parseInt(0)){
		$("#varField_idFacilitador1 option[value='1']").hide();
		$("#varField_idFacilitador1 option[value='2']").show();
		$("#varField_idFacilitador1 option[value='3']").show();
		
		$("#varField_idFacilitador2 option[value='1']").show();	
		$("#varField_idFacilitador2 option[value='2']").show();	
		$("#varField_idFacilitador2 option[value='3']").hide();	
		
		$("#varField_idFacilitador3 option[value='1']").hide();		
		$("#varField_idFacilitador3 option[value='2']").show();
		$("#varField_idFacilitador3 option[value='3']").hide();		
	}	
	
	
		if (parseInt(varField_idFacilitador1) == parseInt(2) && parseInt(varField_idFacilitador2) == parseInt(0) && parseInt(varField_idFacilitador3) == parseInt(3)){
		$("#varField_idFacilitador1 option[value='1']").show();
		$("#varField_idFacilitador1 option[value='2']").show();
		$("#varField_idFacilitador1 option[value='3']").hide();
		
		$("#varField_idFacilitador2 option[value='1']").show();	
		$("#varField_idFacilitador2 option[value='2']").hide();	
		$("#varField_idFacilitador2 option[value='3']").hide();	
		
		$("#varField_idFacilitador3 option[value='1']").show();		
		$("#varField_idFacilitador3 option[value='2']").hide();
		$("#varField_idFacilitador3 option[value='3']").show();		
	}	

		if (parseInt(varField_idFacilitador1) == parseInt(3) && parseInt(varField_idFacilitador2) == parseInt(0) && parseInt(varField_idFacilitador3) == parseInt(2)){
		
		$("#varField_idFacilitador1 option[value='1']").show();
		$("#varField_idFacilitador1 option[value='2']").hide();
		$("#varField_idFacilitador1 option[value='3']").show();
		
		$("#varField_idFacilitador2 option[value='1']").show();	
		$("#varField_idFacilitador2 option[value='2']").hide();	
		$("#varField_idFacilitador2 option[value='3']").hide();	
		
		$("#varField_idFacilitador3 option[value='1']").show();		
		$("#varField_idFacilitador3 option[value='2']").show();
		$("#varField_idFacilitador3 option[value='3']").hide();		
	}		
			
		

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="varField_idFacilitador1" onChange="fnFuncao(this.value);">
  <option value="0">Selecione o Colaborador 1</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

<select id="varField_idFacilitador2" onChange="fnFuncao(this.value);">
  <option value="0">Selecione o Colaborador 2</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

<select id="varField_idFacilitador3" onChange="fnFuncao(this.value);">
  <option value="0">Selecione o Colaborador 3</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

  • There is a way with less code and more dynamic to make this code?

  • 1

    Edit the question and put HTML please.

  • Laercio o o Html são 3 Dropdowns com 3 valores iguais: Os Valores São: id Nome Nome do campo 1 Fábia varField_idFacilitador1 2 Helio varField_idFacilitador2 3 Sueli varField_idFacilitador3

  • puts html bro ... pfvr

  • what is this option:selected ?

  • one more thing, why so many if ? uses else if

  • So that’s the question. I want to make a Loop to go through the values and go through it. But I can’t do it. option:Selected is Jquery. It’s how it treats <select option> Ja is in Html only on the JS/JQUERY part. varField_idFacilitador1 is the Dropdown 1 varField_idFacilitador2 is the Dropdown 2 varField_idFacilitador3 is the Droodown 3

  • WE NEED HTML

  • I put the HTML

  • I saw you use .hide() to hide a <option>. The problem with this is that it doesn’t work in IE (it works in Chrome, I think Firefox tb and I didn’t test in Opera)... so it would be better to do as @bfavaretto proposed, instead of hiding, disable.. or else, if you make a point of hiding even without using .hide(), an idea would be to create an array for each <select> and each time an option is selected, dynamically recreate the <select> pulling the values of your respective array, ignoring the value that is to ignore.

  • But IE here is not going to be a problem. And I’m hiding the value of Option because here it has to be like this. But in this case it would be composite array?

  • Now they want four figures for Dropdown. Like, Fabia, Helio, Sueli, and Lucas. And as it comes from the database can have infinite records per dropdown.

  • So far so good because this dynamic is coming from the bank. However I’m not able to return the values when you select option 0("select").

  • @DVD Cara to perdidaço... And they put me against the wall here. Look where I got: https://jsfiddle.net/toewrmox/88/

Show 9 more comments

2 answers

2


To give you a start, because there’s a lot to improve on my code:

var indisponiveis = [];
$('.select').on('change', function(e) {
  var _select = this;
  var select = $(this);
  var valor = select.val();

  var preenchidos = $('.select').filter( function(i, el){ return el.value > 0 } );
  indisponiveis = $.map(preenchidos, function(el){ return +el.value } ); 
  
  $('.select').each(function(i, el) {
    if(el !== _select) {
       $(el).find('option').each(function(i, op) {
         if(indisponiveis.indexOf(+op.value) > -1) {
           $(op).attr('disabled', true);
         } else {
           $(op).attr('disabled', false);
         }
      });
    } 
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="s1" class="select">
  <option value="0">selecione</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

<select id="s2" class="select">
  <option value="0">selecione</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

<select id="s3" class="select">
  <option value="0">selecione</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

  • Thanks bfavaretto. Gave a good lightening here. I’ll see if I can put the recover part as well because I have to update by Asp too instead of just insert. But I gave it a good whitening.

  • It changed everything here. Now they want here with 4 values per Dropdown. Like: Fabia, Helio, Sueli, and Lucas. And as it comes from the database can have infinite records by dropdown. So far so good. However no return values.

0

I did it this way below. Only now I can’t get him to show again. I’m stuck here now.

 $('.select').on('change', function(e) {
  var _select = this;
  var select = $(this);
  var valor = select.val();
  
  
  var $combo1 = $("#varField_idFacilitador1");
	var $combo2 = $("#varField_idFacilitador2");
	var $combo3 = $("#varField_idFacilitador3");
  
  var indisponiveis = [];
  
  var preenchidos = $('.select').filter( function(i, el){ return el.value > 0 } );
  indisponiveis = $.map(preenchidos, function(el){ return +el.value } ); 
  
 
  $(".select").each(function(i, el){
 			if(el !== _select) {
       $(el).find('option').each(function(i, op) { 		         																	             if(indisponiveis.indexOf(+op.value) > -1) {
       			$("#varField_idFacilitador1 option[value="+valor+"]").hide();
       	   	$("#varField_idFacilitador2 option[value="+valor+"]").hide();
            $("#varField_idFacilitador3 option[value="+valor+"]").hide();
   				}else{
   
         			}
          })
    		}
 			});
  	
     
 });         
       
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="varField_idFacilitador1" class="select" >
  <option value="0">Selecione o Colaborador 1</option>
  <option value="1">Fábia</option>
  <option value="2">Hélio</option>
  <option value="3">Sueli</option>
  <option value="4">Lucas</option>  
</select>

<select id="varField_idFacilitador2" class="select" >
  <option value="0">Selecione o Colaborador 2</option>
  <option value="1">Fábia</option>
  <option value="2">Hélio</option>
  <option value="3">Sueli</option>
  <option value="4">Lucas</option>   
</select>

<select id="varField_idFacilitador3" class="select" >
  <option value="0">Selecione o Colaborador 3</option>
  <option value="1">Fábia</option>
  <option value="2">Hélio</option>
  <option value="3">Sueli</option>
  <option value="4">Lucas</option>   
</select>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.