[英]javascript enable disable radio button on click and select only one at a time
I am trying to do this. 我正在尝试这样做。
I have 3 radio buttons, 我有3个单选按钮,
I want to do in standard javascript without jquery as can't have that at the moment. 我想在没有jQuery的标准javascript中进行操作,因为目前无法做到这一点。
Here's my code so far. 到目前为止,这是我的代码。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<script type="text/javascript">
function toggle(radioBtn)
{
if(radioBtn.checked)
{
radioBtn.checked = false;
}
else
{
radioBtn.checked = true;
}
// only select one at a time.
}
</script>
</head>
<body>
<form id="myForm" name="myForm">
<input type="radio" name="radioBtn" id="radioBtn1" value="A" onClick="toggle(this);" />
<input type="radio" name="radioBtn" id="radioBtn1" value="B" onClick="toggle(this);"/>
<input type="radio" name="radioBtn" id="radioBtn1" value="C" onClick="toggle(this);"/>
</form>
</body>
</html>
Please help. 请帮忙。 Your help will be appreciated.
您的帮助将不胜感激。 Thanks
谢谢
You could just do something simple and straight forward inline, like this: 您可以执行简单直接的内联操作,例如:
<input type="radio" name="radioBtn" id="radioBtn1" value="A" onMouseDown="this.__chk = this.checked" onClick="if (this.__chk) this.checked = false" />
<input type="radio" name="radioBtn" id="radioBtn2" value="B" onMouseDown="this.__chk = this.checked" onClick="if (this.__chk) this.checked = false"/>
<input type="radio" name="radioBtn" id="radioBtn3" value="C" onMouseDown="this.__chk = this.checked" onClick="if (this.__chk) this.checked = false"/>
记录哪些radioButton被选中,哪些未被选中(可以通过隐藏字段或数组来完成),并设置为选中状态/未选中状态
You can get around the browser's default behavior for radio buttons and achieve the toggle effect you're after by creating an additional 'hidden' radio button, and simulating a click
event for it when the user toggles off one of the visible buttons. 您可以绕过浏览器的单选按钮默认行为,并通过创建一个附加的“隐藏”单选按钮,并在用户关闭其中一个可见按钮时为其模拟一个
click
事件,从而获得所需的切换效果。 See a working example in this jsFiddle: http://jsfiddle.net/k73Nt/ 请参阅此jsFiddle中的工作示例: http : //jsfiddle.net/k73Nt/
This might not be a solution which will go into the book for being clean but i got it working. 这可能不是一个干净的解决方案,但我确实可以使用。
http://jsfiddle.net/C9At9/3/ http://jsfiddle.net/C9At9/3/
HTML: HTML:
<input type="radio" name="radioBtn" id="radioBtn1" value="A" onmouseup="toggle(this);"/>
<input type="radio" name="radioBtn" id="radioBtn2" value="A" onmouseup="toggle(this);" />
<input type="radio" name="radioBtn" id="radioBtn3" value="A" onmouseup="toggle(this);"/>
Javascript: Javascript:
function toggle(radioBtn)
{
if(radioBtn.checked)
{
setTimeout("disableRadio('"+radioBtn.id+"')",10);
} else {
radioBtn.checked = true;
}
}
function disableRadio(radioId) {
el = window.document.getElementById(radioId);
el.checked = false;
}
Your code wasn't working because the javascript setter gets overruled by html click events or something. 您的代码无法正常运行,因为javascript设置器被html click事件或其他事件所取代。 I don't know how to explain it the correct way but having a delay solves your problem.
我不知道如何正确解释它,但是稍稍延迟可以解决您的问题。
Hope this helps out! 希望这会有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.