I have a selection element:
-
<select id="selvisatype"
title="Please select a visa type">
<option
value="pleaseselect">Please
Select</option>
<option
value="notapplicable">Not
Applicable</option>
<option value="a1">A-1</option>
<option
value="a2">A-2</option>
<option
value="b1">B-1</option>
. . .
</select>
I want to retrieve the value the user has selected (or
"Please Select" if they've not selected anything). I
tried this:
-
if ($('#visitor').is(':checked')) {
console.log('visitor is checked');
var visaselection =
$('#selvisatype').val();
console.log('visaselection is ' + visaselection);
}
...and, after checking the "visitor" radio button, the
console says "visitor is checked" then "visaselection
is undefined"
This happens whether I leave selvisatype alone or make a
selection from it. It also happens with this code:
- var
visaselection = $('select[name="selvisatype"]').val()
With this code, OTOH:
- var
visaselection = $("#visaselection option:selected").text();
...it simply says, "visaselection is " (blank)
UPDATE
Yet,
according to what's in the URLBar after a form submit:
...it
does recognize a selection there:
...selvisatype=b1...
So I'm trying to get the value via jQuery from the wrong
property or something...