Select Value

0

I have select with multiple car brands and I have a script that when selecting the car should appear its marks. I wanted to update the tags, but when I click the button to update only the last tag and the template chosen is that it updates.

HTML code

<select name='marca'  placeholder='marca' id='marca'onChange='onSelectChange()' '$row->marca' required/>
    <option value ='$row->marca'>$row->marca  - marca do seu veiculo </option></br>
    <option value='Alfa Romeo'>Alfa Romeo</option>
    <option value='Aston Martin'> Aston Martin</option>
    <option value='Audi'>Audi</option>
</select>

Model code

<select size='1' name='modelo' id='modelo0' style='display:none;'>
    <option selected value='modelo'>modelo</option>
    <option value='100'>100</option>
    <option value='80'>80</option>
    <option value='A1'>A1</option>
    <option value='A3'>A3</option>
    <option value='A4'>A4</option>
    <option value='A5'>A5</option>
    <option value='A6'>A6</option>
    <option value='A7'>A7</option>
    <option value='A8'>A8</option>
    <option value='Q2'>Q2</option>
    <option value='Q3'>Q3</option>
    <option value='Q5'>Q5</option>
    <option value='Q7'>Q7</option>
    <option value='R8'>R8</option>
    <option value='TT'>TT</option>
</select>

<select size='1' name='modelo' id='modelo1' style='display:none;'>
    <option selected value='modelo'>modelo</option>
    <option value='asdas'>aasas</option>
    <option value='dasdas'>asdasd</option>
</select>

<select size='1' name='modelo' id='modelo2' style='display:none;'>
    <option selected value='modelo'>modelo</option>
    <option value='dasdasdsa'>sadsadsad</option>
    <option value='asdcccascasas'>aaasdasdsas</option>
    <option value='dasadasdsdas'>dsadasd</option>
</select>

JavaScript code

function onSelectChange() {
    var value = document.getElementById("marca").value;

    if ( (value == 'Audi')) {
        document.getElementById('modelo0').style.display = 'block';
    } else {
        document.getElementById('modelo0').style.display = 'none';
    } 

    if ( (value == 'Alfa Romeo')) {
        document.getElementById('modelo1').style.display = 'block';
    } else {
        document.getElementById('modelo1').style.display = 'none';
    } 

    if ( (value == 'Aston Martin')) {
        document.getElementById('modelo2').style.display = 'block';
    } else {
        document.getElementById('modelo2').style.display = 'none';
    } 
}
    
asked by anonymous 02.06.2017 / 15:12

1 answer

1

I changed the script a little bit, I believe it works the way you expect it, instead of using the if else structure, I used switch case because it is more efficient:

jQuery(function($){
   $('#marca').change(function(){
          var marca = $('#marca').val();    
          switch(marca){ 
		case '':
			$('#modelo0, #modelo1, #modelo2').hide();
			break;
		case 'Audi':
			$('#modelo0').show();
			$('#modelo1, #modelo2').hide();
			break;
		case 'Alfa Romeo':
			$('#modelo1').show();
			$('#modelo0, #modelo2').hide();
			break; 
		case 'Aston Martin':
			$('#modelo2').show();
			$('#modelo0, #modelo1').hide();
			break;
		default:
			$('#modelo0, #modelo1', '#modelo2').hide();
                        break;
        }
   });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select name='marca'  placeholder='marca' id='marca' required/>
 <option value=''>Selecione a marca do seu veiculo </option></br>
 <option value='Alfa Romeo'>Alfa Romeo</option>
 <option value='Aston Martin'> Aston Martin</option>
 <option value='Audi'>Audi</option>
</select>

<select size='1' name='modelo' id='modelo0' style='display:none;'>
    <option selected value='modelo'>modelo</option>
    <option value='100'>100</option>
    <option value='80'>80</option>
    <option value='A1'>A1</option>
    <option value='A3'>A3</option>
    <option value='A4'>A4</option>
    <option value='A5'>A5</option>
    <option value='A6'>A6</option>
    <option value='A7'>A7</option>
    <option value='A8'>A8</option>
    <option value='Q2'>Q2</option>
    <option value='Q3'>Q3</option>
    <option value='Q5'>Q5</option>
    <option value='Q7'>Q7</option>
    <option value='R8'>R8</option>
    <option value='TT'>TT</option>
</select>

<select size='1' name='modelo' id='modelo1' style='display:none;'>
        <option selected value='modelo'>modelo</option>
        <option value='asdas'>aasas</option>
        <option value='dasdas'>asdasd</option>


</select>

<select size='1' name='modelo' id='modelo2' style='display:none;'>
        <option selected value='modelo'>modelo</option>
        <option value='dasdasdsa'>sadsadsad</option>
        <option value='asdcccascasas'>aaasdasdsas</option>
        <option value='dasadasdsdas'>dsadasd</option>
</select>
    
02.06.2017 / 15:25