javascript 如何使用警告框确认单选按钮选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8071651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:09:22  来源:igfitidea点击:

How to confirm a radio button selection with an alert box

javascripthtmlalert

提问by Spartan Man

So i have this code:

所以我有这个代码:

<html>
<head>
<title>Form</title>
<script type="text/javascript">
function showConfirmationDialog() {
    var textbox = document.getElementById('textbox');
    var location = document.getElementById('location');
    alert('You chosen:'+'\n'+'\n'+'Name: '+textbox.value +'\n'+'Address: ' +location.value+'\n');
}

function formfocus()  {
    document.getElementById('textbox').focus();
}
window.onload = formfocus;
var option; 
</script>   
</head>
<body>

Your name: 
<input type="text" name="FirstName" id="textbox" <br><br/> 

Your Address:
<input type="text" name="address" id="location" <br></br><br></br>

Choose your location:

<form name="Radio" id="destination" action="">

Bristol: 

<input type="radio" name="selection" value="bristol" onClick="option=0">

&nbsp;&nbsp;&nbsp; London: 

<input type="radio"  name="selection" value="london" onClick="option=1">

&nbsp;&nbsp;&nbsp; Birmingham:

<input type="radio"  name="selection" value="birmingham" onClick="option=2" />

</form>

<br></br> Click:
<input type="button" value="Submit" onclick="showConfirmationDialog();" /><br></br>

</body>
</html>

... This code basically represents a form for a user to fill in and at the end select one of three option provided via the radio buttons. What I wanted to find out was that how do I get the selection from one radio button which the user will need to select, displayed within the alert box after they press submit.

...此代码基本上代表用户填写的表单,最后选择通过单选按钮提供的三个选项之一。我想知道的是,如何从用户需要选择的一个单选按钮中获取选择,在他们按下提交后显示在警报框中。

采纳答案by jamie-wilson

You need to loop through the selection radios to get the checked value:

您需要循环选择收音机以获取选中的值:

var selection = document.Radio.selection;
var selectionResult = "";
for(var i = 0; i < selection.length; i++) {
    if(selection[i].checked) {
        selectionResult = selection[i].value;
    }
}
alert('You chosen:'+'\n'+'\n'+'Name: '+textbox.value +'\n'+'Address: ' +location.value+'\n' + 'Location: '+selectionResult);

回答by Yevgeny Simkin

Something like this...

像这样的东西...

function getSelRadioValue()    

    for(i = 0; i< document.forms['Radio'].elements['selection'].length ; i++){
            if(document.forms['Radio'].elements['selection'][i].checked == true)
                  return document.forms['Radio'].elements['selection'][i].value;
        }
   return null;
}




 var selectedRadioValue = getSelRadioValue();  //use this variable in your alert.

   if(selectedRadioValue == null)
      alert("please select a destination");
   else if(confirm("You have selected " + selectedRadioValue))
     //deal with success