I have the following form that has 2 selects:
<form id="form" class="form_visitar" method="post" action="ajax/selects.php">
<select name="select-local" class="select-index">
<option value="">'.$select_visitar_tipo.'</option>
<option value="1">'.$select_visitar_rest.'</option>
<option value="2">'.$select_visitar_bar.'</option>
<option value="3">'.$select_visitar_cafe.'</option>
</select>
<select name="select-precio" class="select-index">
<option value="">'.$select_visitar_precio.'</option>
<option value="1">'.$select_visitar_p1.'</option>
<option value="2">'.$select_visitar_p2.'</option>
<option value="3">'.$select_visitar_p3.'</option>
</select>
</form>
What I want to happen is: after I select something from the dropdowns (right after clicking them I have no submit button) I want to get the value to make a query via ajax. I’m very new to using the ajax function and I feel I might not get it as much as I’d want to. This is my ajax function so far (selects.php right now is just a message that will show in a div for testing):
<script type="text/javascript">
$(".form_visitar").click(function() {
$.ajax({
type: "POST",
url: $(this).attr("action"),
data: $(this).serialize(),
success: function(data) {
$("#result").html(data);
}
})
return false;
});
</script>
You need to bind to the
changeevent on the select elements, and not to theclickevent on the form.This will bind the
changehandler to theselectelements with class-namesselect-localandselect-precio. If you want to just target a specific select, just change the selector:If you’re sending just the value of the select, then you probable don’t need to serialize the entire form. You could do something like this: