Reputation: 35
I have the following problem:
When an option from the dropdown list is selected it should be added to the searchbox. This works. But now I want the "GO" button to be clicked when automatically as soon as this has happenned. With the code I have so far, I'm trying to use the
$('input[name="go"]').trigger("click");
part to achieve this. The problem however, is that this last event is triggered as soon as the dropdown box is clicked (and thus before an option can be selected). I tried many things, but I'm not very experienced with jquery. (btw: I'm using a pre-existing search field and button)
This is the javascript/jquery code:
$(document).ready(function(){
$('select[name="Articlecategories"]').bind('click', function() {
$('select[name="Articlecategories"]').change(function () {
var str = "";
$('select[name="Articlecategories"] option:selected').each(function () {
str += $(this).text() + " ";
});
$('input[name="searchfield"]').val(str);
})
.change();
$('input[name="searchfield"]').change(function () {
$('input[name="go"]').trigger("click");
})
.change();
});
});
And this the html:
<select name="Artikelcategorien" >
<option selected="selected" disabled>select an option</option>
<option >Candy</option>
<option>Taffy</option>
<option>Caramel</option>
<option>Fudge</option>
<option>Cookie</option>
</select>
<input type="text" name="searchfield" value="" >
<input type="submit" name="go" value="GO" >
Upvotes: 0
Views: 1900
Reputation: 35
Ok, never mind, I maneged to solve it. Not the most elegant solution perhaps, but it works:
$(document).ready(function(){
$('select[name="Articlecategories"]').change(Klikzoeken)
});
function Klikzoeken() {
$('select[name="Articlecategories"]')
var str = "";
$('select[name="Articlecategories"] option:selected').each(function () {
str += $(this).text();
});
$('input[name="searchfield"]').val(str);
$('input[name="go"]').click();
}
Upvotes: 0
Reputation: 15158
You can just submit the form (presuming that this lives within a form of course):
$('form#formId').submit();
Upvotes: 0
Reputation: 10040
.trigger()
only executes the function attached to certain element not executes default behaviours of that element.
You can use this if have all above in form
element.
$("form").submit();
Upvotes: 1