Skip to content

Instantly share code, notes, and snippets.

@Restoration
Created June 11, 2016 05:51
Show Gist options
  • Select an option

  • Save Restoration/6c94765173c5c06b5aa32839589c867f to your computer and use it in GitHub Desktop.

Select an option

Save Restoration/6c94765173c5c06b5aa32839589c867f to your computer and use it in GitHub Desktop.
セレクトボックスの値を取得する
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<select name="select" id="select">
<option value="1">Value1</option>
<option value="2">Value2</option>
<option value="3">Value3</option>
</select>
<script type="text/javascript">
//JavaScriptを使って取得する
var selects = document.getElementById('select');
var option = document.getElementById('select').options;
var value = option[selects.selectedIndex].text;
//value値で取得するなら
//var value = option[selects.selectedIndex].value;
document.write(value);
//jQueryを使って取得する
jQuery(function($){
$('#select option:selected').text();
$('#select').children(':selected').text();
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment