내가 자꾸 까먹어서 쓰는 개발 이야기/jQuery

Selectbox의 onchange에서 confirm으로 취소했을 시 상태 원복하기

FIL. 2013. 4. 4. 16:05
728x90

var prev_val;

$('#dropdown').focus(function() {
    prev_val = $(this).val();
}).change(function() {
     $(this).blur() // Firefox fix as suggested by AgDude
    var success = confirm('Are you sure you want to change the Dropdown?');
    if(success)
    {
        alert('changed');
        // Other changed code would be here...
    }  
    else
    {
        $(this).val(prev_val);
        alert('unchanged');
        return false; 
    }
});


즉, focus() 이벤트에서 현재 선택값을 저장해두고, 취소했을 시 저장해둔 값으로 설정한다는것.


출처 : http://stackoverflow.com/questions/6677744/jquery-cancel-change-event-on-confirmation-dialog-for-a-dropdown