I have an page with a selection box, the first option of which is please choose, I need to ensure the user has made a selection ignoring the first option.
A:
Give the first option a value "0":
<select id="#myselectionbox">
<option value="0">Please select</option>
...
</select>
and check the selected value:
if ($("#myselectionbox").val() != "0") {
// ok
}
Philippe Leybaert
2010-10-01 12:36:26
A:
You could use something like this:
if ($("#yourselectid").val() == "Please ignore") {
alert("You must select another option.");
}
Vasileios Lourdas
2010-10-01 12:36:57
+1
A:
You can use .selectedIndex (faster) or .index() for this:
if($("#selectID")[0].selectedIndex == 0) {
alert("Please choose something");
}
//or no jQuery at all:
if(document.getElementById("selectID").selectedIndex == 0) {
Or the .index() of the selected <option>, much slower but works:
if($("#selectID :selected").index() == 0) {
alert("Please choose something");
}
Nick Craver
2010-10-01 12:39:46