我创建了三个单选按钮。我想运行一个函数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");
}
}
答案 0 :(得分:3)
该功能可以更简单。您不必检查所有按钮,因为您可以将当前按钮传递到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
答案 1 :(得分:2)
我认为你可以总结一下:
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对象。
答案 2 :(得分:1)
取消选择时,您需要循环显示单选按钮以清除它们。您的代码中也有一些小的语法错误,这些错误在下面进行了更正:
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 example 强>
答案 3 :(得分:1)
你可以这样使用 -
<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>