4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaScriptで複数のラジオボタンを連動させる記述。

いいえを選択すると下のラジオが受付不能になります

DEMO

記述

html
<article>
	<header><h1>いいえを選択すると下のラジオが受付不能になります</h1></header>
	<section class="checkbox_area">
		<p>血液はありますか?</p>
		<form action="xxxxxx" method="post" name="frm_yesno">
		<div><input type="radio" id="in_yesno1" name="q[1]" value="1" onclick="rCheck('1')">はい</div>
		<div><input type="radio" id="in_yesno2" name="q[1]" value="0" onclick="rCheck('0')" checked>いいえ</div>
		</form>

	</section>
	<section class="btn_area">
		<p>では何型ですか?</p>
		<form action="xxxxxx" method="post">
		<div><input type="radio" name="type_blood" id="r_btnA" value="a">A型</div>
		<div><input type="radio" name="type_blood" id="r_btnB" value="b">B型</div>
		<div><input type="radio" name="type_blood" id="r_btnO" value="o">O型</div>
		<div><input type="radio" name="type_blood" id="r_btnAB" value="ab">AB型</div>
		<div><input type="radio" name="type_blood" id="r_btnA" value="unknown" disabled="disabled">不明</div>
		</form>

	</section>

</article>

javascript
function rCheck(chkID) {
	var array = ['r_btnA', 'r_btnB', 'r_btnO', 'r_btnAB'];
	for (kay in array){
		var r_btn = document.getElementById(array[kay]);
		if(chkID == '0'){
			r_btn.setAttribute('disabled', 'disabled');
			r_btn.checked = false;
		} else {
			r_btn.removeAttribute('disabled');
		}
	}
}
window.onload = function(){
	var array = ['in_yesno1', 'in_yesno2'];
	var ynFlag = '1';
	for (kay in array){
		var r_btn = document.getElementById(array[kay]);
		if(r_btn.checked){
			ynFlag = r_btn.getAttribute('value');
			break;
		}
	}
	rCheck(ynFlag);
};
4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?