Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Почему код не выполняется при type="image" а при type="radio" выполняется? (https://javascript.ru/forum/misc/79658-pochemu-kod-ne-vypolnyaetsya-pri-type%3D-image-pri-type%3D-radio-vypolnyaetsya.html)

Улан 05.03.2020 13:38

Почему код не выполняется при type="image" а при type="radio" выполняется?
 
Надеюсь за обращение вопросами не будете ругаться. Тут мне уже помогали еще с утра, а дальше все еще мучаюсь. был шаблон radio кнопками, теперь вместо radio необходимо было вставить картинки, пробовал менять все не получается. да согласен сам еще тупой пока по js, но для меня это и есть практика. подскажете?https://codepen.io/tu03422/pen/PoqJJqb?editors=1010

рони 05.03.2020 14:01

Улан
сделайте через классы.

document.imageControl.imgChange это только type="radio в данном случае


и на всякий случай
<input type="image"> - это кнопка отправки формы

voraa 05.03.2020 15:30

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" lang="ru">
  <meta name="viewport" content="width=device-width, initial-scale=1.0" >
  <title>TESTE </title>
<style>
/* Прячем input за img */
label>img {
	position:relative;
	height: 100px;
	z-index: 1;
}
label>input {
	position:relative;
	left: -30px;
	top: -30px;
}
</style>
<script>
</script>
</head>

<body>
<div class="input_block">  
    <legend>Выберите вариант</legend>

<form name="imageControl">
<fieldset style="border: 2px solid #1221; border-radius: 10px; width:500px; height:250px;">
<label for="im1">
<img  src="http://html-plus.in.ua/wp-content/uploads/examples/js-examples/cups/cup4.png" >
<input type="radio" class="input_img" name="imgChange" id="im1" value="http://html-plus.in.ua/wp-content/uploads/examples/js-examples/cups/cup4.png">
</label>
<label for="im2">
<img  src="http://html-plus.in.ua/wp-content/uploads/examples/js-examples/cups/cup2.jpg" >
<input type="radio" class="input_img" name="imgChange" id="im2" value="http://html-plus.in.ua/wp-content/uploads/examples/js-examples/cups/cup2.jpg">
</label>
<label for="im3">
<img  src="http://html-plus.in.ua/wp-content/uploads/examples/js-examples/cups/cup3.jpg" >
<input type="radio" class="input_img" name="imgChange" id="im3" value="http://html-plus.in.ua/wp-content/uploads/examples/js-examples/cups/cup3.jpg">
</label>
<br>
<label for="im4">
<img  src="http://html-plus.in.ua/wp-content/uploads/examples/js-examples/cups/cup1.jpg" >
<input type="radio" class="input_img" name="imgChange" id="im4" value="http://html-plus.in.ua/wp-content/uploads/examples/js-examples/cups/cup1.jpg">
</label>
<label for="im5">
<img  src="http://html-plus.in.ua/wp-content/uploads/examples/js-examples/cups/cup1.jpg" >
<input type="radio" class="input_img" name="imgChange" id="im5" value="http://html-plus.in.ua/wp-content/uploads/examples/js-examples/cups/cup1.jpg">
</label>
<label for="im6">
<img  src="http://html-plus.in.ua/wp-content/uploads/examples/js-examples/cups/cup2.jpg" >
<input type="radio" class="input_img" name="imgChange" id="im6" value="http://html-plus.in.ua/wp-content/uploads/examples/js-examples/cups/cup2.jpg">
</label>
</fieldset>
</form>

</div>
<div>
	<img id='imres' >
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
	document.querySelectorAll('input[type=radio]').forEach (inp => {
		inp.addEventListener('change', (ev) => {
			let ichecked = document.querySelector('input[type=radio]:checked')
			document.getElementById('imres').src = ichecked.value 
		})
	})
})
</script>
</body>
</html>

Улан 06.03.2020 06:19

Огромное Вам спасибо! еще раз спасибо! и много раз спасибо! я бы давно перестал изучать этот пока непонятный мне JS без таких как ВЫ людей добрых!!!


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