티스토리 뷰

Programming/JavaScript, Sencha

jQuery select box 관련 처리

파란크리스마스 2015. 1. 30. 10:05
728x90

출처 : jQuery select box 관련 api

동적으로 Option 추가

$.each(result.data.list, function(index, data)
{
	try {
		var row = $('#row_item').clone();
		row.appendTo('.uio_text');
						
		// JQuery에서 Select에 추가
		var findItem = $("select[name='item_list'] option[value='"+data.tml+"']");
		if (data.tml!=undefined && $("select[name='item_list'] option").index(findItem)<0) {
			$("select[name='item_list']").append("<option value='"+data.tml+"'>"+data.tml+"</option>");
		}
	} catch(err) {
		alert(err.message);
	}
});

선택된 Value 구하기

var item_id = $("select[name='item_list'] option:selected").val();

선택된 Index 구하기

var index = $("select[name='item_list'] option").index($("select[name='item_id'] option:selected"));

Value값으로 Index 구하기

var index = $("select[name='item_list'] option").index($("select[name='item_list'] option[value='value1']"));

Text값으로 Index 구하기

	// 방법1
	/*
	$('select[name="space_id"] option').filter(function() { 
		return $.trim( $(this).text() ) == '${ho_info.ho_line}'; 
	}).attr('selected','selected');
	*/
	
	var index = -1;
	$("select[name='space_id'] option").each(function () {
		if ($(this).html() == '${ho_info.ho_line}') {
			$(this).prop("selected", "selected");
			index = $(this).index();
			return;
		}
	});
	
	if (index>=0) {
		$("#lbl_selected_space").attr('class','label').css('color', 'black');
	} else {
		$("#lbl_selected_space").show();
		$("select[name='space_id'] option:eq(0)").attr("selected", "selected");
	}

개수 구히기

var count = $("select[name='item_list'] option").size();


댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함