我試圖通過AJAX在選擇框更改時自動提交搜索過濾器表單。問題是在整個#filter
表格div上使用.change
。如果我在#address
輸入中輸入文字,則不會顯示結果。一旦我改變了選擇值,結果就會顯示出來。它可以很好地使用.submit
,但需要它自動發送。自動提交AJAX搜索表單上的選擇過濾器更改
JS
$('#filter').change(function(e){
e.preventDefault();
var filter = $('#filter');
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
var elat = place.geometry.location.lat();
var elng = place.geometry.location.lng();
filter.append($('<input>',{type:'hidden', name:'elat'}).val(elat));
filter.append($('<input>',{type:'hidden', name:'elng'}).val(elng));
});
$.ajax({
url: filter.attr('action'),
data:filter.serialize(),
type: 'POST', // POST
dataType: 'json',
success: function(response) {
showCloseLocations(response);
$('#response').html("");
}
});
return false;
}).change();
FORM(使用WordPress)
function search_form()
{
$address = $_GET['address'];
$widlat = $_GET['elat'];
$widlng = $_GET['elng']; ?>
<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">
<input id="address" name="property_location" placeholder="Enter a city, country or postcode" type="text" value='<?php echo $address; ?>' required>
<input type="hidden" id="elat" name="elat" value='<?php echo $widlat; ?>'/>
<input type="hidden" id="elng" name="elng" value='<?php echo $widlng; ?>'/>
<select name="radius_miles" id="radius_km">
<option value=1>+1 miles</option>
<option value=2>+2 miles</option>
<option value=5>+5 miles</option>
<option value=30 selected>+30 miles</option>
</select>
<input type="hidden" name="action" value="ek_search">
</form>
<?php
}
add_action('sco_search_form', 'search_form');
還有一個'.keyup()'函數,你可以做的使用:https://api.jquery.com/keyup/ –
文本框不執行「更改」事件。所以你需要使用別的東西來捕捉這些。 keyup是一個合理的選擇,雖然你可能想要阻止它自動發送,直到輸入了3個字符,所以你不要通過搜索「a」等來超載你的服務器。 – ADyson