Reputation: 489
I am using the code shown below to create a confirmation box when a particular option is selected from a <select>
dialog. I am able to get the confirm dialog just fine when the "bar" option is selected. However, if I cancel out of it, the selected option goes to blank. If I first select another option, then select "bar" and cancel, it goes back to the previously selected option (this is the desired behavior). Why does the first cancel fail while subsequent cancels do not?
<html>
<head>
<title>Test of jQuery confirm select</title>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
$(function(){
$('#example').change(function() {
var selected = $(this).val();
if (selected == 'bar') {
if (!confirm('Are you sure?')) {
$(this).val($.data(this, 'current')); // added parenthesis (edit)
return false;
}
}
$.data(this, 'current', $(this).val());
});
});
</script>
</head>
<select id="example">
<option value="none" selected="selected">--Select an Option--</option>
<option value="foo">foo</option>
<option value="bar">bar</option>
<option value="que">que</option>
</select>
<body>
</body>
</html>
Just to to be clear, if I go to "bar" from any other option this does not occur. But it always occurs if "bar" is the first option I select.
Upvotes: 0
Views: 140