var ISearch = ISearch || {}; var b_line_sta_click = false; ISearch.setup = function(textfield, callback, options) { var timer = null, lastInput = ''; wait = 500; minLength = 0; if (options !== undefined) { wait = options.wait; minLength = options.minLength; } textfield.on('keyup change', function() { var input = $.trim($(this).val()); if (lastInput !== input && input.length >= minLength) { clearTimeout(timer); timer = setTimeout(function() { callback(input); }, wait); } lastInput = input; }); }; $(function() { // 沿線、駅取得 ISearch.setup($('#line_sta_inp'), function(input) { b_line_sta_click = false; $('#line_sta_sel_box').hide(); $('#lin').val(''); $('#sta').val(''); $.get('/search/line_sta_sub.php', { q: $('#line_sta_inp').val() } ).done(function( json_data ) { if(json_data !== null){ var tmp_html = '
'; for(var i in json_data){ tmp_html += '
' + json_data[i].name + '
'; tmp_html += '
' + json_data[i].lin + ',' + json_data[i].sta + '
'; } tmp_html += '
'; $('#line_sta_sel_box').html(tmp_html); $('#line_sta_sel_box dl dt').click(function(){ $('#line_sta_inp').val($(this).text()); var sa_data = $(this).next('dd').text().split(','); if(sa_data.length == 2){ $('#lin').val(sa_data[0]); $('#sta').val(sa_data[1]); b_line_sta_click = true; } $('#line_sta_sel_box').hide(); }); $('#line_sta_sel_box').show(); } }); }); $('#search_frm').submit(function(){ // 駅・沿線を入力して一覧から選択しない場合エラー if( !b_line_sta_click ){ alert('入力後、駅名または沿線名を選択してください。'); return false; } }); });