[英]Inline JavaScript enable/disable select and text box on click radio button
[英]javascript enable disable radio button on click and select only one at a time
我正在尝试这样做。
我有3个单选按钮,
我想在没有jQuery的标准javascript中进行操作,因为目前无法做到这一点。
到目前为止,这是我的代码。
<!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>
请帮忙。 您的帮助将不胜感激。 谢谢
您可以执行简单直接的内联操作,例如:
<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被选中,哪些未被选中(可以通过隐藏字段或数组来完成),并设置为选中状态/未选中状态
您可以绕过浏览器的单选按钮默认行为,并通过创建一个附加的“隐藏”单选按钮,并在用户关闭其中一个可见按钮时为其模拟一个click
事件,从而获得所需的切换效果。 请参阅此jsFiddle中的工作示例: http : //jsfiddle.net/k73Nt/
这可能不是一个干净的解决方案,但我确实可以使用。
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:
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;
}
您的代码无法正常运行,因为javascript设置器被html click事件或其他事件所取代。 我不知道如何正确解释它,但是稍稍延迟可以解决您的问题。
希望这会有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.