[英]Disabling and enabling button submit based on radio input conditions
如果用戶未選擇單選按鈕,我想禁止輸入字段可單擊。 這是簡單的HTML表單:
<form method="POST">
<input type='radio' name='a' value='a' id='checkMe' /> a
<input type='radio' name='a' value='b' id='checkMe' /> b
<input type='radio' name='a' value='c' id='checkMe' /> c
<input type='submit' value='choose' id='choose' disabled="disabled"/>
</form>
現在,我制作了這個js,以查看是否選擇了輸入之一,那么應該驗證disabled="disabled"
部分,但是現在在此JavaScript代碼中就是這種情況
if(document.getElementById('checkMe').checked) {
document.getElementById('choose').disabled=false;
}
這是在線演示。 http://jsfiddle.net/2HC6s/
<!DOCTYPE html>
<html>
<head>
<title>Stack</title>
<script>
function set_btn_status()
{
var radios = document.getElementsByName("a");
for (var i = 0; i < radios.length; i++) {
if (radios[i].checked) {
var checked_value = radios[i].value;
if(checked_value == 'a') {
document.getElementById('choose').disabled = false;
} else {
document.getElementById('choose').disabled = true;
}
break;
}
}
}
</script>
</head>
<body>
<form method="POST">
<input type='radio' name='a' value='a' id='checkMe' onclick="set_btn_status()"/> a
<input type='radio' name='a' value='b' id='checkMe1' onclick="set_btn_status()"/> b
<input type='radio' name='a' value='c' id='checkMe2' onclick="set_btn_status()"/> c
<input type='submit' value='choose' id='choose' disabled="disabled"/>
</form>
</body>
</html>
試試這個 演示
<form method="POST" id="question">
<input type='radio' name='a' value='a' id='checkMe' onclick="check()"/> a
<input type='radio' name='a' value='b' id='checkMe1' onclick="check()" /> b
<input type='radio' name='a' value='c' id='checkMe2' onclick="check()" /> c
</br>
function check()
{
var ele = document.getElementsByName('a');
var flag=0;
for(var i=0;i<ele.length;i++)
{
if(ele[i].checked)
flag=1;
}
if(flag==1)
document.getElementById('choose').disabled=false;
}
把它放在桌子上,然后在她身上做:
var tabPom = document.getElementById("pomTableId");
$(tabPom ).prop('disabled', true/false);
<form method="POST" id="question">
<input type='radio' name='a' value='a' id='checkMe' /> a
<input type='radio' name='a' value='b' id='checkMe' /> b
<input type='radio' name='a' value='c' id='checkMe' /> c
</br>
<input type='submit' value='choose' id='choose' disabled="disabled"/>
</form>
<script>
document.getElementById('checkMe').onclick = function() {
document.getElementById('choose').disabled=false;
}
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.