克隆并附加在jquery mobile-倍增?

时间:2011-08-08 21:13:47

标签: jquery-mobile append clone

当我在jquery mobile中克隆并追加/添加html片段时,片段正在加倍。您可以插入此代码并进行测试。

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1"> 

    <!-- standard Jquery/jQuery Mobile Libraries -->
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0b2/jquery.mobile-1.0b2.min.css" />
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/mobile/1.0b2/jquery.mobile-1.0b2.min.js"></script>   
    </head> 

    <body>  
    <div data-role="page" id="mainmenu">
    <div data-role="header" data-position="inline"></div>

    <div class="ui-body ui-body-c">
        <div data-role="content">   

        <a href="#" class="preShowHTML">click to view HTML</a>      
        <pre>
        <span id="HTMLOut">
        my HTML output goes here...
        </span>
        </pre>
    </div>
    </div>  


    <div id='groupA' class='preGroups'>         
        <div id='placeholder' ></div>   
    </div>

    <fieldset class="ui-grid-a" data-inline="true">     
        <div class="ui-block-b"><button type="submit"  class="addPart" data-theme="a" data-icon="plus">Add Serial/Part</button></div>
    </fieldset>

    <div id='template'>                 
        <div data-role="fieldcontain">
            <input type="range" name="QTY" id="preQuant01" value="1" min="1" max="10"/>
        </div>

    </div>
</div>

<style>
#template,  #HTMLOut, #XMLOut{
    display:none;
}
</style>


<script>
counter = 1;

$(document).ready(function() {

/* Add a listeners to Add Part */   
    $('.addPart').click(function() {    
            myClone = $('#template').clone();           
            myClone.attr("id", "template-" + counter);  
            counter++;
            myClone.appendTo("#placeholder").trigger( "create" );
            // myClone.appendTo("#placeholder").page(); does not work in this version?

        return false;
    });   

    // Toggle Show/Hide HTML
    $('.preShowHTML').click(function() {    
         $("#HTMLOut").text($("body").html());
         $("#HTMLOut").toggle();
        return false;
    });             
});
</script>

2 个答案:

答案 0 :(得分:0)

AJAX可能会让它略显清晰,但这是一个开始:

$(document).ready(function(){
    $("div").live("pageshow", function () { 
        counter = 0;
        $(".addPart").click(function() {    
                counter++;
                $("#placeholder").append('<div  id="template-'+counter+'"><div data-role="fieldcontain"><input type="range" name="slider" class="ui-slider-input ui-input-text ui-body-null ui-corner-all ui-shadow-inset ui-body-c" id="slider'+counter+'" value="0" min="0" max="100" /></div></div>');
                $("#template-"+counter).trigger("create");

            return false;
        });   

    });
});
</script>

答案 1 :(得分:0)

我认为柜台在错误的地方。然而,这并不能使示例更具功能性。

myClone = $('#template').clone();           
myClone.attr("id", "template-" + counter);  
//counter++;
myClone.appendTo("#placeholder");
$('#template-'+counter).page();                
counter++;

我不确定这是正确的方法 - 在jquery mobile应用它的监听器和格式化之后克隆html。