You can't assign a new value to the result of a function
$('input#q').val() = urlencode($('input#q').val());
Use this instead:
$('input#q').val(urlencode($('input#q').val()))
It wouldn't work with the keypress either - maybe the page is simply submitted after the same js error occurs.