Autocomplete Plugin - Passing in a Checkbox Value
I'm using the autocomplete plugin from this tutorial:
http://viralpatel.net/blogs/2009/06/tutorial-create-autocomplete-feature-with-java-jsp-jquery.html
Environment is Windows XP 5.1.2600, Firefox 3.6.8 and Chrome 5.0.375
Code Snippets:
<input type="text" id="country" name="country"/>
<input type="checkbox" name="mycheck" id="mycheck" />
<script>
$("#mycheck").click(function() {
alert($('input[name=mycheck]:checked').val());
});
$("#country").autocomplete(
"getdata.jsp",{
cacheLength:0,
minChars:1,
max:10,
autoFill:false,
extraParams: {
mycheck: function() { return $('input[name=mycheck]:checked').val(); }
}
});
</script>
getdata.jsp looks like:
<%
DummyDB db = new DummyDB();
String query = request.getParameter("q");
List<String> countries = db.getData(query);
Iterator<String> iterator = countries.iterator();
while (iterator.hasNext()) {
String country = (String) iterator.next();
out.println(country);
}
%>
If I use this line in getdata.jsp:
String param= request.getParameter("mycheck");
The value I get back is:
function() { return $('input[name=mycheck]:checked').val();
If I change my extraParams to this:
extraParams: {
mycheck: function() { return 'hello, world!' }
}
The value I get back is:
function() { return 'hello, world!' }
It seems to interpret the function as a static value, and doesn't actually execute it. Of course, I can't just simply use this:
extraParams: {
mycheck: $('input[name=mycheck]:checked').val();
}
as the AutoComplete plugin always returned it’s initial value. I've found quite a few helpful links online about this, notable this one:
http://blog.qumsieh.ca/2009/09/08/jquery-autocomplete-extraparams-and-checkboxes/
Hence the above code using a function which I posted, but can't get that to work. Any help or hints would be helpful. Thanks in advance!