gpt4 book ai didi

javascript - 如何设置数组以便在不使用 jQuery 的情况下获取单选按钮值

转载 作者:行者123 更新时间:2023-11-28 07:30:32 26 4
gpt4 key购买 nike

我在设置数组以获取单选按钮的值时遇到问题。这些是我的表单中的单选按钮:

<form name="artistImages"> //This is the form tag 
<legend> Gallery </legend>
<input type="radio" name="artist" value="Z"><img src="zedd.png" height="82" width="68">
<input type="radio" name="artist" value="T"><img src="taylorswift.jpg" height="82" width="68">
<input type="radio" name="artist" value="P"><img src="pharrell.jpg" height="82" width="68">
<input type="radio" name="artist" value="B"><img src="beyonce.jpg" height="82" width="68">
<input type="radio" name="artist" value="D"><img src="drake.jpg" height="82" width="68">
<input type="radio" name="artist" value="E"><img src="eminem.jpg" height="82" width="68">
<input type="button" id="mybutton" value="GetValue" onClick="getRadioValue(radioArray)"/><br>

<input id="image" type="text" name="name" size="20">

这是 JavaScript

<script>
var radioArray = [document.forms.artistImages.artist];
function getRadioValue(radioArray){
var i
var text = document.getElementById('image').value;

for (i = 0; i < radioArray.length; i++){
if(radioArray[i].checked) return radioArray[i].value;
var text = radioArray[i].value;
};
};
</script>

最佳答案

使用此脚本:

<script>
function getRadioValue(){
if(document.getElementById("artist1").checked)
document.getElementById("image").value = document.getElementById("artist1").value;
if(document.getElementById("artist2").checked)
document.getElementById("image").value = document.getElementById("artist2").value;
if(document.getElementById("artist3").checked)
document.getElementById("image").value = document.getElementById("artist3").value;
if(document.getElementById("artist4").checked)
document.getElementById("image").value = document.getElementById("artist4").value;
if(document.getElementById("artist5").checked)
document.getElementById("image").value = document.getElementById("artist5").value;
if(document.getElementById("artist6").checked)
document.getElementById("image").value = document.getElementById("artist6").value;
};
</script>

和这个 html:

<form name="artistImages">
<legend> Gallery </legend>
<input type="radio" id="artist1" name="artist" value="Z"><img src="zedd.png" height="82" width="68">
<input type="radio" id="artist2" name="artist" value="T"><img src="taylorswift.jpg" height="82" width="68">
<input type="radio" id="artist3" name="artist" value="P"><img src="pharrell.jpg" height="82" width="68">
<input type="radio" id="artist4" name="artist" value="B"><img src="beyonce.jpg" height="82" width="68">
<input type="radio" id="artist5" name="artist" value="D"><img src="drake.jpg" height="82" width="68">
<input type="radio" id="artist6" name="artist" value="E"><img src="eminem.jpg" height="82" width="68">
<input type="button" id="mybutton" value="GetValue" onClick="getRadioValue();"/><br>

<input id="image" type="text" name="name" size="20">
</form>

关于javascript - 如何设置数组以便在不使用 jQuery 的情况下获取单选按钮值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29172652/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com