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

标签 javascript html alert

所以我有这个代码:

<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>

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

最佳答案

像这样的……

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

关于javascript - 如何使用警告框确认单选按钮选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8071651/

相关文章:

javascript - node.js 将数组值写入文本文件

html - 左右切换不显示导航

html - 页脚在底部但不粘

php - 使用 php 警报 mysql 列的评论

javascript - 表单数据在 Angular 4 http 请求的 Request Payload 中似乎是其他内容

javascript - 如何缩放 SVG

javascript - 在 HTML 的不同部分使用相同的 ng-controller 和不同的别名

javascript - 如果不满足条件语句,则触发引导模式

iphone - 如何在 iPhone/iPad/iOS 上显示临时弹出消息

javascript - 将导航中的下拉子菜单对齐到与其父级相同的高度