How to select a select input after form submission in Jquery mobile?
I have a form with a input. When I submit my form and go back to it, this input goes back to the default value.
Usually i do this :
$(document).ready(function(){
$('#selectCountry').val('<?php echo $country ?>');
});
BUT in JQuery mobile, due to Ajax wrapping, $(document).ready does not fire after f开发者_JS百科orm submission.
Is there a way to resolve this simple but annoying problem ?
Updated
It would be better if you can load the value on page load.
<select id="selectCountry">
<?php
foreach ($countries as $country){
echo '<option value="' . $country . '"';
if($country == $selectedCountry){
echo ' selected="selected"';
}
echo '>' . $country . '</option>';
}
?>
</select>
Update again
This is how you do it for jquery mobile.
$('#selectCountry').live('pagecreate',function(){
$(#'selectCountry').val('<?php echo $country; ?>');
});
See this page.
Previous suggestion - Can you use the .live() function?
$('#selectCountry').live(function(){ $(#'selectCountry').val(''); });
Change this:
$(document).ready(function(){
$('#selectCountry').val('<?php echo $country ?>');
});
to this:
$(document).ready(function(){
init()
});
function init() {
$('#selectCountry').val('<?php echo $country ?>');
}
Then at the bottom of your page you can always call init() directly:
<script type='text/javascipt'>
init()
</script>
精彩评论