如何使用警告框确认单选按钮的选择
How to confirm a radio button selection with an alert box
所以我有这个代码:
<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">
London:
<input type="radio" name="selection" value="london" onClick="option=1">
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>
…这段代码基本上代表了一个供用户填写的表单,并在最后从单选按钮提供的三个选项中选择一个。我想知道的是,如何从用户需要选择的单选按钮中获得选择,在他们按下submit后显示在警告框中。
像这样…
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
您需要遍历选择无线电以获得选中的值:
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);
相关文章:
- 根据页面加载时的单选按钮选择显示某些字段
- Ng点击记住单选按钮选择
- Angular未更新单选按钮选择
- 阅读JS中的单选按钮选择
- JQuery IE11 单选按钮选择器
- 确认单选按钮选择
- 贝宝表单问题的单选按钮选择
- 使用javascript在单选按钮选择上加载URL
- 显示基于多个单选按钮选择的图像
- 基于输入值的单选按钮选择
- 将域类id返回到由单选按钮选择的控制器
- 无法更改单选按钮选择 - KnockoutJS
- 单选按钮选择列表框更改并将 NULL 值传递给每个列表框
- 用户在Javascript的帮助下,仅通过单选按钮选择一个付款方式(信用卡)
- 基于多个单选按钮选择显示或隐藏元素
- 如何根据单选按钮选择在表中切换多个输入
- 无法更改单选按钮选择
- 提交页面而不为所需单选按钮选择值
- 使用基于单选按钮选择的 jquery 将表格单元格添加到行尾
- j查询动态验证单选按钮选择