Javascript 如何在不使用jQuery的情况下设置数组以获取单选按钮值
我在设置数组以获取单选按钮的值时遇到问题。以下是我表格中的单选按钮:Javascript 如何在不使用jQuery的情况下设置数组以获取单选按钮值,javascript,html,radio-button,Javascript,Html,Radio Button,我在设置数组以获取单选按钮的值时遇到问题。以下是我表格中的单选按钮: //这是表单标记 图库 这就是javascript var radioArray=[document.forms.artistImages.artist]; 函数getRadioValue(radioArray){ 变量i var text=document.getElementById('image')。值; 对于(i=0;i
//这是表单标记
图库
这就是javascript
var radioArray=[document.forms.artistImages.artist];
函数getRadioValue(radioArray){
变量i
var text=document.getElementById('image')。值;
对于(i=0;i
使用此脚本:
<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>
函数getRadioValue(){
if(document.getElementById(“artist1”).已选中)
document.getElementById(“image”).value=document.getElementById(“artist1”).value;
if(document.getElementById(“artist2”).已选中)
document.getElementById(“image”).value=document.getElementById(“artist2”).value;
if(document.getElementById(“artist3”).已选中)
document.getElementById(“image”).value=document.getElementById(“artist3”).value;
如果(选中document.getElementById(“artist4”))
document.getElementById(“image”).value=document.getElementById(“artist4”).value;
if(选中document.getElementById(“artist5”))
document.getElementById(“image”).value=document.getElementById(“artist5”).value;
if(选中document.getElementById(“artist6”))
document.getElementById(“image”).value=document.getElementById(“artist6”).value;
};
这个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>
画廊
完成任务的另一个简化/通用版本:
<script>
function getRadioValue(){
for (var i = 0; i < document.forms[0].length-2; i++){
if(document.forms[0][i].checked)
document.getElementById("image").value = (document.forms[0][i].value);
};
};
</script>
函数getRadioValue(){
对于(var i=0;i
以及HTML:
<form name="artistImages">
<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();"/><br>
<input id="image" type="text" name="name" size="20">
</form>
画廊
好的,我已经编辑了它,如果有必要的话,我的单选按钮之间也有一个字段集。好的,所以有一系列单选按钮,当选中一个单选按钮并单击“mybutton”时,它应该将该单选按钮值放入“image”文本框中。但是,当我选择一个单选按钮并单击“mybutton”按钮时,文本框中没有显示任何内容。我已经更新了答案,希望它是您想要的@谢谢,这正是我需要的:DNo问题@Noctis。我的荣幸
<form name="artistImages">
<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();"/><br>
<input id="image" type="text" name="name" size="20">
</form>