Jquery Mobile problem with AJAX updates
I'm having a very weird problem with radio buttons in JQuery Mobile. I'm populating some radiobuttons with ajax. When I do it the first time it's ok, but any subsequent loads seem to cause problems with the display - each checkbox is displayed separately rather than on a single list.
function getWords() {
var gig_id = $('#gigs').val();
$.ajax({
url: Nnn.serverLocation+'/words?gigid='+ gig_id,
success: function(data) {
Nnn.words = eval('(' + data + ')');
displayWords();
}
});
}
function displayWords() {
$('#word_container').html('<fieldset data-role="controlgroup" id="words"></fieldset>');
$('#words').html("<legend>It's:</legend>");
$.each(Nnn.words, function(key, value) {
$('#words').append('<label for="'+value.Word+'" >'+value.Word+'</label><input type="radio" value="'+value.Word+'" id="'+value.Word+'" name="radio-choice-1" />');
});
$('#words input').checkboxradio();
$('body').page();
}
The HTML looks like
<开发者_StackOverflow;div id='all' data-role="page">
<div data-role="content">
<div data-role="fieldcontain" id='word_container'>
<fieldset data-role="controlgroup" id='words'>
</fieldset>
</div>
</div>
It's driving me crazy!
The problem is with the proper load/display of CSS in the page. If you reload the page using Ajax, you can not use HTML5 data-* attributes. For example, button control in jQueryMobile is represented like this:
<a href="index.html" data-role="button">Link button</a>
If you use the same markup in the ajax call, it won't be displayed properly. What you have to do is, Firefox/Chrome/Opera->Right Click->Inspect Element and use the markup shown there. So, the proper markup for the button control would be:
<a class="ui-btn ui-btn-corner-all ui-shadow ui-btn-hover-c" data-role="button" href="index.html" data-theme="c">
<span class="ui-btn-inner ui-btn-corner-all">
<span class="ui-btn-text">Link button</span>
</span>
</a>
精彩评论