从无线电选择中查找标签 img 并应用于单独的 img src



我正在寻找从selected input中获取img src并将其应用于我在.selected-radio下创建的单独div img srcJavaScript

我希望.selected-radioimg srcselection和初始checked相匹配。

#radios {
	display: flex;
	padding: 20px;
}
#radios label,
.selected-radio {
	display: block;
	height: 38px;
	width: 38px;
	cursor: pointer;
	position: relative;
}
#radios label + label {
	margin-left: 25px;
}
input[type="radio"] {
	opacity: 0;
	position: absolute;
}
input[type="radio"] + span {
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	color: #b3cefb;
	border-radius: 50%;
	padding: 5px;
	transition: all 0.4s;
	-webkit-transition: all 0.4s;
}
input[type="radio"]:checked + span {
	border: 2px solid #4285f4;
}
<div class="selected-radio"><img src="https://uploads-ssl.webflow.com/57e5747bd0ac813956df4e96/5a985a90ec8f79000104514a_united-kingdom.svg">
</div>
<div class="container">
	<div id="radios">
		<label for="usa">
			<input type="radio" name="mode" id="usa" value="usa" checked/>
			<span><img src="https://uploads-ssl.webflow.com/57e5747bd0ac813956df4e96/5a2f052996bde90001f96632_united-states-of-america.svg"><span>
		</label>
		<label for="canada">
			<input type="radio" name="mode" id="canada" value="canada" />
			<span><img src="https://uploads-ssl.webflow.com/57e5747bd0ac813956df4e96/5a2cd7b0937442000184b147_canada.svg"><span>
		</label>
		<label for="uk">
			<input type="radio" name="mode" id="uk" value="uk" />
			<span><img src="https://uploads-ssl.webflow.com/57e5747bd0ac813956df4e96/5a985a90ec8f79000104514a_united-kingdom.svg"><span>
		</label>
	</div>
</div>

你可以用javascript做到这一点。

我添加了一个函数,changeImg ,并在 img 上放了一些 id。

img的 Id 采用特定形式,以"img-"开头,以相关单选按钮的值结束。

function changeImg(elm) {
  var val = elm.value;
  
  var img = document.getElementById('img-' + val);
  
  var src = img.src;
  
  var imgSelectedRadio = document.getElementById('img-selected-radio');
  
  imgSelectedRadio.src = src;
}
#radios {
	display: flex;
	padding: 20px;
}
#radios label,
.selected-radio {
	display: block;
	height: 38px;
	width: 38px;
	cursor: pointer;
	position: relative;
}
#radios label + label {
	margin-left: 25px;
}
input[type="radio"] {
	opacity: 0;
	position: absolute;
}
input[type="radio"] + span {
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	color: #b3cefb;
	border-radius: 50%;
	padding: 5px;
	transition: all 0.4s;
	-webkit-transition: all 0.4s;
}
input[type="radio"]:checked + span {
	border: 2px solid #4285f4;
}
<div class="selected-radio"><img id="img-selected-radio" src="https://uploads-ssl.webflow.com/57e5747bd0ac813956df4e96/5a985a90ec8f79000104514a_united-kingdom.svg">
</div>
<div class="container">
	<div id="radios">
		<label for="usa">
			<input type="radio" onchange="changeImg(this)" name="mode" id="usa" value="usa" checked/>
			<span><img id="img-usa" src="https://uploads-ssl.webflow.com/57e5747bd0ac813956df4e96/5a2f052996bde90001f96632_united-states-of-america.svg"><span>
		</label>
		<label for="canada">
			<input type="radio" onchange="changeImg(this)"  name="mode" id="canada" value="canada" />
			<span><img id="img-canada" src="https://uploads-ssl.webflow.com/57e5747bd0ac813956df4e96/5a2cd7b0937442000184b147_canada.svg"><span>
		</label>
		<label for="uk">
			<input type="radio" onchange="changeImg(this)"  name="mode" id="uk" value="uk" />
			<span><img id="img-uk" src="https://uploads-ssl.webflow.com/57e5747bd0ac813956df4e96/5a985a90ec8f79000104514a_united-kingdom.svg"><span>
		</label>
	</div>
</div>

最新更新