我创建了三个单选按钮。我想运行一个功能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
Java脚本
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对象。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句