Created
September 8, 2014 12:52
-
-
Save tanatana/faabff092fc49357807e to your computer and use it in GitHub Desktop.
jQueryの.each()のやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<meta charset="utf-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>$.each()のやつ</title> | |
</head> | |
<body> | |
<h1>部員リスト</h1> | |
<!-- この学校部活のid3桁で管理してるっぽいけど999個以上の部活動できたらどうするのかなとか考えちゃうよね --> | |
<select id="clubSelector"> | |
<option>select</option> | |
<option value="cl001">サッカー部</option> | |
<option value="cl002">野球部</option> | |
<option value="cl003">吹奏楽部</option> | |
</select> | |
<ul id="memberlist"></ul> | |
新しいウィンドウの名前: <input id='newWindowName' type="text"> <br> | |
<a href="#" id="openWindow">開く</a> | |
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> | |
<!-- Include all compiled plugins (below), or include individual files as needed --> | |
<script> | |
$(document).ready(function(){ | |
var clubMembers = { | |
'cl001': ['st13010', 'st13011', 'st14012'], | |
'cl002': ['st13100', 'st14100', 'st14005'], | |
'cl003': ['st12023', 'st13045', 'st14009'] | |
}; | |
var students = { | |
'st13010': {'name': 'A', 'grade': 2, 'class': 'A', 'sex': 'F'}, | |
'st13011': {'name': 'B', 'grade': 2, 'class': 'A', 'sex': 'M'}, | |
// 'st14012': {'name': 'C', 'grade': 1, 'class': 'A', 'sex': 'M'}, | |
'st13100': {'name': 'D', 'grade': 2, 'class': 'A', 'sex': 'M'}, | |
'st14100': {'name': 'E', 'grade': 1, 'class': 'A', 'sex': 'F'}, | |
'st14005': {'name': 'F', 'grade': 1, 'class': 'A', 'sex': 'M'}, | |
'st12023': {'name': 'G', 'grade': 3, 'class': 'A', 'sex': 'F'}, | |
'st13045': {'name': 'H', 'grade': 2, 'class': 'A', 'sex': 'M'}, | |
'st14009': {'name': 'I', 'grade': 1, 'class': 'A', 'sex': 'F'} | |
}; | |
var clubMembersDetail = function(clubName){ | |
var detail = []; | |
$.each(clubMembers[clubName], function(){ | |
detail.push(students[this]); | |
}); | |
return detail; | |
}; | |
$('#clubSelector').on('change', function () { | |
var clubName = this.value | |
$('ul#memberlist').empty(); | |
$.each(clubMembersDetail(clubName), function(){ | |
$('ul#memberlist').append('<li>' + this.name + '</li>') | |
}); | |
}); | |
$('#openWindow').on('click', function(){ | |
window.open('./', $('#newWindowName').val()); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment