Skip to content

Instantly share code, notes, and snippets.

@springcome
Created October 18, 2013 02:01
Show Gist options
  • Save springcome/7035363 to your computer and use it in GitHub Desktop.
Save springcome/7035363 to your computer and use it in GitHub Desktop.
select, jquery
<select name="selectName" id="selectId">
<option value="1">1번</option>
<option value="2">2번</option>
<option value="3">3번</option>
</select>
// selected된 값을 알고자 할때
$('#selectId option:selected').val();
// value로 selected하고자 할때
$('#selectId > option[value=1]').attr('selected', true);
// selected한 index를 알고자 할때
$('#selectId option').index($('#selectId option:selected'));
// index로 value값을 알고자 할때
$('#selectId option:eq(1)').val();
// index로 selected하고자 할때
$('#selectId option:eq(1)').attr('selected', true);
// selected한 option삭제
$('#selectId option:selected').remove();
// option 추가
var option = $('<option />').attr('value', 4).html('4번');
$('#selectId').append(option);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment