My mobile page has 2 "pages" (using the multi-page template of jQuery Mobile). Now i have a language selector (dropdown). This selector appears on both pages.
Relevant code:
<html>
<head>
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="scripts/style/jquery.mobile-1.3.1.min.css" />
<script src="scripts/javascript/jquery-1.9.1.min.js"></script>
<script src="scripts/javascript/jquery.mobile-1.3.1.min.js"></script>
<script src="scripts/javascript/translations.js"></script>
</head>
<body>
<div class="container">
<form action="#" method="POST">
<div class="formContent" data-role="page" id="one">
<div data-role="header" class="ui-header-fixed">
<div data-role="navbar">
<ul>
<li></li>
<li><label for="language" class="select"></label> <select
name="language" id="language" class="lang" data-corners="false"
data-iconpos="noicon" onchange="trans($(this));">
<option value="english">En</option>
<option value="dutch">Nl</option>
<option value="german">De</option>
</select></li>
</ul>
</div>
</div>
<div class="content">
<!-- Content -->
</div>
<div data-role="footer" data-id="foo1" data-position="fixed">
<div data-role="navbar">
<ul>
<li><a href="#one" class="ui-disabled">Vorige</a></li>
<li><a href="#two">Volgende</a></li>
</ul>
</div>
</div>
</div>
<div class="formContent" data-role="page" id="two">
<div data-role="header" class="ui-header-fixed">
<div data-role="navbar">
<ul>
<li></li>
<li><label for="language" class="select"></label> <select
name="language" id="language" class="lang" data-corners="false"
data-iconpos="noicon" onchange="trans($(this));">
<option value="english">En</option>
<option value="dutch">Nl</option>
<option value="german">De</option>
</select>
</li>
</ul>
</div>
</div>
<div class="content">
<!-- Content -->
</div>
<div data-role="footer" data-id="foo1" data-position="fixed">
<div data-role="navbar">
<ul>
<li><a href="#one">Vorige</a></li>
<li><a href="#two" class="ui-disabled">Volgende</a></li>
</ul>
</div>
</div>
</div>
</form>
</div>
<script>
translate($('.lang').val());
function trans(it){
var v = it.val();
translate(v);
$(".lang option[value='" + v + "']").attr('selected', 'selected');
}
</script>
</body>
</html>
translate(); contains the translation. The last line of the trans() function should set the selected option of both selectors.
Now when i select a language on the first page, the selected value on both selectors change. But when i select a language on the second page, the selected value does not change on both selectors.
Why is this and how do i solve it?
Edit
Finally got that working:
$(document).on('change', '#language', function () {
var v = $(this).val();
translate(v);
$("#language option").removeAttr('selected');
$("#language option[value='" + v + "']").attr('selected', 'selected');
$("#language").selectmenu("refresh");
});
Now the select menu's don't refresh. In the source code (using firebug) the correct item is selected, but in the front-end it displays the wrong item. Seems like selectmenu('refresh'); doesn't do the trick.
Solved
I ended up setting the display value myself:
$(document).on('change', '#language', function () {
var v = $(this).val();
translate(v);
$("#language option").removeAttr('selected');
$("#language option[value='" + v + "']").attr('selected', 'selected');
$(".ui-header .ui-navbar .ui-grid-a .ui-block-b .ui-select .ui-btn .ui-btn-inner .ui-btn-text span").html(v);
});
Don't mind the long selector, i just want to be sure i have the correct dropdown ;)