Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   вспытие окошек при выборе select (https://javascript.ru/forum/misc/74077-vspytie-okoshek-pri-vybore-select.html)

Menshakovich 11.06.2018 19:50

вспытие окошек при выборе select
 
Добрый вечер!Помогите пожалуйста!!!
Дело в том, что мне нужно при выборе определенного числа, высвечивались окошечки, число которые равно выбранному числу из select.
Вот код. но явно что-то не так(
div class="line_buttom">       <select class="size_of_buttoms" name="child" onchange="ChildCount(this)">
                        <option value="0">0</option>
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                        <option value="4">4</option>
                        <option value="5">5</option>
                    </select>
                    возраст
                    <span class="size_of_buttoms">
                    <input name="agechild[]" class="agechild" style="display: none;" type="text">
                    <input name="agechild[]" class="agechild" style="display: none;" type="text">
                    <input name="agechild[]" class="agechild" style="display: none;" type="text">
                    <input name="agechild[]" class="agechild" style="display: none;" type="text">
                    <input name="agechild[]" class="agechild" style="display: none;" type="text">
                    </span>
                        </div>


и скрипт
<script type="text/javascript">
    function ChildCount(select)
    {
    var a=select.value;
        
    
        if (a==1)
    {       document.getElementByTagName("agechild[]").style.display="block";
            document.getElementById("agechild[]").style.display="none";
            document.getElementById("agechild[]").style.display="none";
            document.getElementById("agechild[]").style.display="none";
            document.getElementById("agechild[]").style.display="none";
    }
        if (a==2)
    {       document.getElementByTagName("agechild[]").style.display="block";
            document.getElementByTagName("agechild[]").style.display="block";
            document.getElementById("agechild[]").style.display="none";
            document.getElementById("agechild[]").style.display="none";
            document.getElementById("agechild[]").style.display="none";
    }       
        if (a==3)
        {   document.getElementByTagName("agechild[]").style.display="block";
            document.getElementByTagName("agechild[]").style.display="block";
            document.getElementByTagName("agechild[]").style.display="block";
            document.getElementById("agechild[]").style.display="none";
            document.getElementById("agechild[]").style.display="none";
        }   
        if (a==4)
        {   document.getElementByTagName("agechild[]").style.display="block";
            document.getElementByTagName("agechild[]").style.display="block";
            document.getElementByTagName("agechild[]").style.display="block";
            document.getElementByTagName("agechild[]").style.display="block";
            document.getElementById("agechild[]").style.display="none";
        }
        if (a==5)
        {   document.getElementByTagName("agechild[]").style.display="block";
            document.getElementByTagName("agechild[]").style.display="block";
            document.getElementByTagName("agechild[]").style.display="block";
            document.getElementByTagName("agechild[]").style.display="block";
            document.getElementByTagName("agechild[]").style.display="block";
        }
    
    }
    </script>

j0hnik 11.06.2018 20:01

<div class="line_buttom">       
		<select class="size_of_buttoms" name="child">
			<option value="0">0</option>
			<option value="1">1</option>
			<option value="2">2</option>
			<option value="3">3</option>
			<option value="4">4</option>
			<option value="5">5</option>
		</select>
		возраст
		<span class="size_of_buttoms">
			<input name="agechild[]" class="agechild" style="display: none;" type="text">
			<input name="agechild[]" class="agechild" style="display: none;" type="text">
			<input name="agechild[]" class="agechild" style="display: none;" type="text">
			<input name="agechild[]" class="agechild" style="display: none;" type="text">
			<input name="agechild[]" class="agechild" style="display: none;" type="text">
		</span>
	</div>


	<script>
		document.querySelector('.size_of_buttoms').onchange = e => document.querySelectorAll('.agechild').forEach((el,i)=>el.style.display = i<+e.target.value?'block':'none');
	</script>

Menshakovich 11.06.2018 20:23


На данном сайте все запускается, но при вставки в html-файл не работает( С чем это связано? Аж по словам копировал и никак:(

j0hnik 11.06.2018 20:27

document.addEventListener("DOMContentLoaded", function(){
document.querySelector('.size_of_buttoms').onchange = e => document.querySelectorAll('.agechild').forEach((el,i)=>el.style.display = i<+e.target.value?'block':'none');
});


так попробуйте, если не заработает смотрите в консоль
(может есть в html такие же классы у других блоков)

Menshakovich 11.06.2018 20:49

я и тут запустил... не работает
https://jsfiddle.net/w25zfLm3/17/

Menshakovich 11.06.2018 20:49

извините,что так не могу разобраться. только только начал(((

Menshakovich 11.06.2018 21:02

уже все убрал. оставил это. на сайте данном работает https://jsfiddle.net/w25zfLm3/20/ а на компе у меня нет(
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>form</title>
<link href="./css/style.css" rel="stylesheet">
<script>
	document.addEventListener("DOMContentLoaded", function(){
document.querySelector('.size_of_buttoms').onchange = e => document.querySelectorAll('.agechild').forEach((el,i)=>el.style.display = i<+e.target.value?'block':'none');
});
	</script>
	
   
</head>

 <body>

<div class="line_buttom">       
<select class="size_of_buttoms" name="child">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
		возраст
<span class="size_of_buttoms">
<input name="agechild[]" class="agechild" style="display: none;" type="text">
			<input name="agechild[]" class="agechild" style="display: none;" type="text">
			<input name="agechild[]" class="agechild" style="display: none;" type="text">
			<input name="agechild[]" class="agechild" style="display: none;" type="text">
			<input name="agechild[]" class="agechild" style="display: none;" type="text">
		</span>
	</div>



			
			
</body>
</html>

Menshakovich 11.06.2018 21:07

Извиняюсь!Все работает!ОГРОМНОЕ ВАМ СПАСИБО!!!


Часовой пояс GMT +3, время: 22:43.