Jquery Mobile problem with AJAX updates

半城伤御伤魂 提交于 2019-12-12 12:26:18

问题


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

<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!


回答1:


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>


来源:https://stackoverflow.com/questions/5346424/jquery-mobile-problem-with-ajax-updates

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!