2014-11-04 33 views
1

我正在使用jQuery顯示一個選擇字段,如果其他值等於'文檔'。如果選擇選項等於值持續

$('#fitler-types').on('change', function() { 
    if($(this).attr('value') == 'document'){ 
     $('#filter-document-type').show('slow'); 
    } 
}); 

這個偉大的工程,但一旦我提交表單的值,但堅持的選擇框被隱藏,只顯示如果我不選文檔,然後重新選擇它。

+0

如果你添加if塊到頁面加載事件? – HJ05 2014-11-04 23:17:23

回答

0

只需在第一次分配處理程序時調用它。

$('#fitler-types').on('change', function() { 
    if($(this).attr('value') == 'document'){ 
     $('#filter-document-type').show('slow'); 
    } 
}).change(); 

堆棧片段演示

$('#fitler-types').on('change', function() { 
 
    $(this).val("change handler called"); 
 
}).change();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input id="fitler-types" />