如何确认所选单选按钮
How to confirm the chosen radio button?
我已经创建了三个单选按钮。我想运行一个函数OnClick,要求用户确认所选的单选按钮,并告知选中的单选按钮值是多少。此外,如果用户不确认选中的单选键,则单选按钮将被取消选择。这是我写的但失败的代码。
<input id="a1" type="radio" name="type" value="A1" onClick= "confirmation();" />A1
<input id="a2" type="radio" name="type" value="A2" onClick="confirmation();" />A2
<input id="a3" type="radio" name="type" value="A3" onClick="confirmation();"/>A3
Javascript
function confirmation() {
if (document.getElementById('a1').checked ) {
ty = document.getElementById('a1').value
var ask= confirm("You have chosen " + ty + " as your type 'n If you have chosen the right type, Click Ok! " )
}
if (document.getElementById('a2').checked) {
level = document.getElementById('a2').value;
var ask= confirm("You have chosen " + ty + " as your Examination Level 'n If you have chosen the right type, Click Ok! " )
}
if (document.getElementById('a3').checked) {
ty = document.getElementById('r1').value;
var ask= confirm("You have chosen " + ty + " as your type 'n If you have chosen the right type, Click Ok! " )
}
if (ask==true) {
alert("You clicked ok");
}
if (ask==false) {
alert("You clicked cancel");
}
}
我认为您可以将其总结为:
function confirmation(obj) {
obj.checked ? confirm("You have chosen " + obj.value + " as your type 'n If you have chosen the right type, Click Ok!") ? alert("You clicked ok") : obj.checked = false : "";
}
<input id="a1" type="radio" name="type" value="A1" onClick="confirmation(this);" />A1
<input id="a2" type="radio" name="type" value="A2" onClick="confirmation(this);" />A2
<input id="a3" type="radio" name="type" value="A3" onClick="confirmation(this);" />A3
将this
传递给函数。this
是指DOM对象。
函数可以更简单。您不必检查所有按钮,因为您可以将当前按钮传递到类似onclick="confirmation(this)"
的函数中。若要取消选中单选按钮,您需要将checked
属性设置为false
。
function confirmation(radio) {
var ask = confirm("You have chosen " + radio.value + " as your type 'n If you have chosen the right type, Click Ok! ")
if (ask) {
alert("You clicked ok");
}
else {
alert("You clicked cancel");
radio.checked = false;
}
}
<input id="a1" type="radio" name="type" value="A1" onclick="confirmation(this)" />A1
<input id="a2" type="radio" name="type" value="A2" onclick="confirmation(this)" />A2
<input id="a3" type="radio" name="type" value="A3" onclick="confirmation(this)" />A3
取消选择时,您需要循环浏览单选按钮以清除它们。您的代码中也有一些小的语法错误,下面将进行更正:
var ty;
function confirmation() {
if (document.getElementById('a1').checked) {
ty = document.getElementById('a1').value
var ask = confirm("You have chosen " + ty + " as your type 'n If you have chosen the right type, Click Ok! ")
}
if (document.getElementById('a2').checked) {
ty = document.getElementById('a2').value;
var ask = confirm("You have chosen " + ty + " as your Examination Level 'n If you have chosen the right type, Click Ok! ")
}
if (document.getElementById('a3').checked) {
ty = document.getElementById('a3').value;
var ask = confirm("You have chosen " + ty + " as your type 'n If you have chosen the right type, Click Ok! ")
}
if (ask == true) {
alert("You clicked ok");
}
if (ask == false) {
var elem = document.getElementsByName('type');
for(var i=0;i<elem.length;i++) elem[i].checked = false;
alert("You clicked cancel");
}
}
jsFiddle示例
您可以像这样使用-
<input type="radio" value="1" onclick="return confirmation()" name="collections">
<input type="radio" value="2" onclick="return confirmation()" name="collections">
<input type="radio" value="3" onclick="return confirmation()" name="collections">
<script>
function confirmation(){
if(confirm("Are you sure!"))
{
//do your job
}
else
{
return false;
}
}
</script>
相关文章:
- 多个单选按钮组相互干扰
- 是否可以禁用jquery中的单个单选按钮
- Javascript:无法获取变量中的单选按钮值
- 通过ajax到php文件获取单选按钮值
- 互斥单选按钮和相应的输入字段
- 用于选择MVC3中单选按钮值的JavaScript
- 如何获取单选按钮的多个实例的选定单选按钮值
- react testUtils模拟点击单选按钮而不触发onchange
- 当选择组中的单选按钮时,jQuery addClass
- 确认单选按钮选择
- 正在寻找特定的JS方法来确认单选按钮的选择
- 如果用户不接受JS确认消息,如何检查上一个选定的单选按钮
- 单选按钮的确认
- 带有使用 JQuery 的单选按钮的确认对话框
- 如何确认所选单选按钮
- 如何取消用户的单选按钮选择,在确认对话框后取消选择
- 如何使用警告框确认单选按钮的选择
- 单选按钮确认,取消和确定做同样的事情
- 确认选择单选按钮
- JavaScript单选按钮确认