I have a question about jQuery. Let's say I have a dropdown list:
<select id="Positions" name="PositionType">
<option value="Manager">Manager</option>
<option value="Janitor">Janitor</option>
<option value="Cook">Cook</option>
<option value="Cashier">Cashier</option>
<option value="Other">Other</option>
</select>
How could I display an alert box when "other" is selected? So far this does not work:
<script type="text/javascript">
$(document).ready(function(){
$("#position").change(function(){
if($("#position option:sel开发者_开发技巧ected").val() == "other"){
alert("hello world");
}
else {
}
});
});
</script>
Two things. First, as others have mentioned, "other" needs to be uppercase as string comparison is case sensitive. Second, you could retrieve the value of a select much easier:
<script type="text/javascript">
$(document).ready(function(){
$("#position").change(function(){
if($(this).val() == "Other"){
alert("hello world");
}
else {
}
});
});
</script>
A select element's value is equal to the value of it's selected option :-)
In your script, try "Other"
with capital-O.
精彩评论