【Labs】JavaScriptでラジオボタンとチェックボックスの入力判定
こんにちは(・∀・)
今日はJavaScriptでラジオボタンとチェックボックスの入力判定を作ってみましたのでご紹介します。
Contents
1. JavaScriptでラジオボタンの入力判定
ラジオボタンの入力判定サンプルです。
HTML
<div class="tb-cell sample-form">
<form id="form">
<div class="row">
<div class="cell">
選択してください
<!--cell--></div>
<div class="cell">
<label><input type="radio" name="radio[]" value="1"> A</label><br>
<label><input type="radio" name="radio[]" value="2"> B</label><br>
<label><input type="radio" name="radio[]" value="3"> C</label><br>
<label><input type="radio" name="radio[]" value="4"> D</label>
<p id="output"></p>
<!--cell--></div>
<!--row--></div>
<div class="row">
<div class="cell">
<!--cell--></div>
<div class="cell">
<button type="button" id="sbtn">submit</button>
<button type="reset" id="rbtn">reset</button>
<!--cell--></div>
<!--row--></div>
</form>
<!--tb-cell--></div>
JavaScript
<script>
function radio(){
var str="";
for(var num=0; num<document.forms['form'].elements['radio[]'].length; ++num){
if(document.forms['form'].elements[num].checked){
if(str !='') str=str+',';
str=str+document.forms['form'].elements[num].value;
}
}
target = document.getElementById('output');
if(str!=''){
target.innerHTML = '<p id="output2">' + str + 'が選択されました</p>';
return false;
}else{
target.innerHTML = '<p id="output3">選択してください</p>';
return false;
}
}
var sbtn = document.getElementById('sbtn');
sbtn.addEventListener('click',radio,false);
function resetall() {
target.innerHTML = '';
return false;
}
var rbtn = document.getElementById('rbtn');
rbtn.addEventListener('click',resetall,false);
</script>
CSS
.tb-cell {
display: table;
width: 300px;
margin: 20px auto 40px auto;
text-align: left;
}
.tb-cell .row {
display: table-row;
}
.tb-cell .row .cell {
display: table-cell;
border: 1px solid #ddd;
padding: 10px;
vertical-align: middle;
color: #fff;
}
.tb-cell .row .cell:nth-child(odd) {
width: 100px;
}
.tb-cell .row .cell:nth-child(even) {
width: 200px;
}
.sample-form .row .cell {
padding: 5px;
}
.sample-form .row .cell:nth-child(1) {
background: #9fb7d4;
}
.sample-form .row .cell:nth-child(2) {
background: #ccc;
}
button {
color: #fff;
border: none;
padding: 10px;
font-size: 16px;
cursor: pointer;
}
button[type="button"] {
background: #afc6e2;
}
button[type="reset"] {
background: none;
}
button[type="button"]:hover {
background: #ddd;
}
button[type="reset"]:hover {
text-decoration: underline;
}
label {
margin-right: 10px;
}
#output {
color: #fff;
}
#output2 {
color: #00f;
}
#output3 {
color: #f00;
}
@media (min-width: 768px) {
.tb-cell {
width: 800px;
}
.tb-cell .row {
display: table-row;
}
.tb-cell .row .cell {
display: table-cell;
border: 1px solid #ddd;
padding: 10px;
vertical-align: middle;
color: #fff;
}
.tb-cell .row .cell:nth-child(odd) {
width: 200px;
}
.tb-cell .row .cell:nth-child(even) {
width: 600px;
}
/* ** */}
Result
サンプルデモはこちら
スマホでのご確認はこちらをどうぞ
2. JavaScriptでチェックボックスの入力判定
チェックボックスの入力判定サンプルです。
HTML
<div class="tb-cell sample-form">
<form id="form">
<div class="row">
<div class="cell">
選択してください
<!--cell--></div>
<div class="cell">
<label><input type="checkbox" name="cbox[]" value="1"> A</label><br>
<label><input type="checkbox" name="cbox[]" value="2"> B</label><br>
<label><input type="checkbox" name="cbox[]" value="3"> C</label><br>
<label><input type="checkbox" name="cbox[]" value="4"> D</label>
<p id="output"></p>
<!--cell--></div>
<!--row--></div>
<div class="row">
<div class="cell">
<!--cell--></div>
<div class="cell">
<button type="button" id="sbtn">submit</button>
<button type="reset" id="rbtn">reset</button>
<!--cell--></div>
<!--row--></div>
</form>
<!--tb-cell--></div>
JavaScript
<script>
function checkbox(){
var str='';
for(var num=0; num<document.forms['form'].elements['cbox[]'].length; ++num){
if(document.forms['form'].elements[num].checked){
if(str !='') str=str+',';
str=str+document.forms['form'].elements[num].value;
}
}
target = document.getElementById('output');
if(str!=''){
target.innerHTML = '<p id="output2">' + str + 'が選択されました</p>';
return false;
}else{
target.innerHTML = '<p id="output3">選択してください</p>';
return false;
}
}
var sbtn = document.getElementById('sbtn');
sbtn.addEventListener('click',checkbox,false);
function resetall() {
target.innerHTML = '';
return false;
}
var rbtn = document.getElementById('rbtn');
rbtn.addEventListener('click',resetall,false);
</script>
CSS
.tb-cell {
display: table;
width: 300px;
margin: 20px auto 40px auto;
text-align: left;
}
.tb-cell .row {
display: table-row;
}
.tb-cell .row .cell {
display: table-cell;
border: 1px solid #ddd;
padding: 10px;
vertical-align: middle;
color: #fff;
}
.tb-cell .row .cell:nth-child(odd) {
width: 100px;
}
.tb-cell .row .cell:nth-child(even) {
width: 200px;
}
.sample-form .row .cell {
padding: 5px;
}
.sample-form .row .cell:nth-child(1) {
background: #9fb7d4;
}
.sample-form .row .cell:nth-child(2) {
background: #ccc;
}
button {
color: #fff;
border: none;
padding: 10px;
font-size: 16px;
cursor: pointer;
}
button[type="button"] {
background: #afc6e2;
}
button[type="reset"] {
background: none;
}
button[type="button"]:hover {
background: #ddd;
}
button[type="reset"]:hover {
text-decoration: underline;
}
label {
margin-right: 10px;
}
#output {
color: #fff;
}
#output2 {
color: #00f;
}
#output3 {
color: #f00;
}
@media (min-width: 768px) {
.tb-cell {
width: 800px;
}
.tb-cell .row {
display: table-row;
}
.tb-cell .row .cell {
display: table-cell;
border: 1px solid #ddd;
padding: 10px;
vertical-align: middle;
color: #fff;
}
.tb-cell .row .cell:nth-child(odd) {
width: 200px;
}
.tb-cell .row .cell:nth-child(even) {
width: 600px;
}
/* ** */}
Result
サンプルデモはこちら
スマホでのご確認はこちらをどうぞ
関連リンク
【Labs】クリックしやすいラジオボタンとチェックボックス
【Labs】チェックしたら有効になるリンク