Reputation: 17721
I have a (multiple) select in my JQM page.
I need to force option with value 'A' de-selection on any other option selection.
HTML is like this:
<select name="select-1" id="select-1" multiple="multiple" data-native-menu="false" />
<option value='A'>a</option>
<option value='B'>b</option>
<option value='C'>c</option>
</select>
I'm using some code like this, with no success... :-(
$("select#select-1").change(function() {
$("select#select-1").val('A').attr("selected", false).trigger("refresh");
});
It looks like the option I'm trying to de-select is selected, and all others are de-selected (excluded the current one) ... :-(
A jsfiddle is here.
Upvotes: 1
Views: 1896
Reputation: 6293
I played around a little bit with your problem and assuming I did not misunderstand your problem (see comments), this snippets works (however probably there are simpler solutions...)
If option with value='B' is selected, then option with value='A' will be deselected.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>if B selected, deselect A</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
</head>
<body>
<!-- if B selected, deselect A -->
<div data-role="page">
<div data-role="content">
<select name="select-1" id="select-1" multiple="multiple" data-native-menu="false" />
<option id="option-A" value='A'>a</option>
<option id="option-B" value='B'>b</option>
<option id="option-C" value='C'>c</option>
</select>
</div><!-- /content -->
</div><!-- /page -->
<script>
$("#select-1").change(function() {
var mySelection = $(this).val();
if (mySelection !== null) {
if (mySelection.indexOf('B') >= 0) {
$("#option-A").attr("selected", false);
$('#select-1').selectmenu('refresh');
};
};
});
</script>
</body>
</html>
EDIT updated now with $('#select-1').selectmenu('refresh');
and it now also works in the jsfiddle
Upvotes: 2