2017-10-15 84 views
1

我沒有早期的jQuery移動體驗,現在我正嘗試在javascript中動態添加新複選框。jQuery手機:動態添加複選框

使用(在HTML頭)HTML的

jquery.mobile-1.4.5.min.css 
jquery-1.11.3.min.js 
jquery.mobile-1.4.5.min.js 

部分:JAVASCRIPT的

<form> 
    <fieldset data-role="collapsible"> 
     <div id="agree" data-role="controlgroup"></div> 
     <input type="submit" value="Submit"> 
    </fieldset> 
</form> 

部分:

var html = '<input type="checkbox" name="checkbox-1" id="checkbox-1" /><label for="checkbox-1">I agree</label>'; 
$('#agree').html(html); 
$("input[type='checkbox']").checkboxradio("refresh"); 

我只得到純複選框,沒有任何格式。 有什麼不對?我錯過了什麼嗎?

+0

請您分享一個工作片段,也許JSFiddle,以便我們知道您正在使用哪個版本的JQuery? –

+0

這裏有一些完整的工作示例:[在jQuery Mobile中動態創建帶有複選框的產品列表](https://stackoverflow.com/questions/41325900/dynamically-creating-productlists-with-checkboxes-in-jquerymobile-proves-difficu/41484070 #41484070)和在這裏:[Jquery Mobile checkbox-list在添加項目和更新時不更新:「刷新」腳本樣式](https://stackoverflow.com/questions/41404047/jquery-mobile-checkbox-list-沒有更新時添加項目和刷新t/41409019#41409019) – deblocker

回答

0

試試這個方法:

$("[data-role=controlgroup]").enhanceWithin().controlgroup("refresh"); 

我希望這可以幫助你!

+0

它的工作原理!謝謝jcarrera。 –