2013-02-06 4 views
0

나는 필드를 선택하고 그들의 부모 폼이 자동으로 제출되기를 원합니다. .자바 스크립트 : 양식 제출 지연

var tmr; 
var timerOn = false; 

function submitSearchOptions() { 
    $('#searchoptionsform').submit(); 
} 

$('#searchoptionsform select').change(function() { 
    if (!timerOn) { 
     tmr = setTimeout(submitSearchOptions,1500); 
     timerOn = true; 
    } 
}); 

$('#searchoptionsform select').click(function() { 
    if (timerOn) { 
     clearTimeout(tmr); 
     timerOn = false; 
    } 
}); 

그러나 그것은 작동하지 않습니다

나는이 시도. select에서 값을 선택하면 타이머를 중지시키는 click 이벤트가 발생합니다.

+1

다만, setTimeout은 항상 0이 아닌 숫자를 반환합니다. 따라서 타이머가 작동하지 않는 경우'tmr = 0'을 사용하여'tmr'과'timerOn'을 원하는대로 조합 할 수 있습니다. –

+0

좋은 팁, 고마워. – L84

답변

1

select은 타이머를 시작한 시간을 추적 할 수 있으며 타이머 시작의 50ms 이내에있는 경우 click을 무시합니다.

var tmr; 
var timerSelect = null; 
var timerStarted = null; 

function submitSearchOptions() { 
    $('#searchoptionsform').submit(); 
} 

$('#searchoptionsform select').change(function() { 
    if (!timerStarted) { 
     tmr = setTimeout(submitSearchOptions,1500); 
     timerStarted = new Date(); 
     timerSelect = this; 
    } 
}); 

$('#searchoptionsform select').click(function() { 
    if (timerStarted) { 
     if (timerSelect !== this || (new Date() - timerStarted) > 50) { 
      clearTimeout(tmr); 
      timerStarted = null; 
      timerSelect = null; 
     } 
    } 
}); 
+0

고마워, 그거야. 좋은 생각 :) – L84

관련 문제