对话框'动态内容只显示一次后,第一个按钮点击

dialog'dynamic content shows only once after first button click

本文关键字:一次 第一个 按钮 动态 显示 对话框      更新时间:2023-09-26

我试图显示一个列表到一个对话框后,一个按钮单击,列表的内容是从服务器检索,我的列表显示当我第一次单击按钮,但随后的对话框显示为空。

onclick的代码正在工作,每次单击按钮时都运行adelement。

<ons-button modifier = "small" style="margin-left: 25%;" 
                  onclick="menu.setMainPage('default.html',{closeMenu: true});
                  ons.createDialog('dialog.html').then(function(dialog) {dialog.show({callback : function(){addPresentation();}});});
                  console.log('log');
                  "
                  > 
                  new  presentation 
               </ons-button>

         <ons-template id="dialog.html">
            <ons-dialog var = "dialog" id = "dialog" cancelable>
             <ons-scroller style="height: 200px; width: 100%">     
               <div  class="dialog-content" id="diagcontent" >

               </div>
             </ons-scroller>
            </ons-dialog>
         </ons-template>

我甚至试过这样做:

   <ons-button modifier = "small" style="margin-left: 25%;" 
      onclick="addElement();
      ons.createDialog('dialog.html').then(function(dialog) {dialog.show();});
      "
      > 

再次给出相同的结果。

Js方法:

   function addPresentation(){

      if (window.XMLHttpRequest){
         var xmlHTTP = new XMLHttpRequest();
         xmlHTTP.open("GET", addpresentation, true);
         xmlHTTP.send();

         xmlHTTP.onreadystatechange = function() {
           var script = document.createElement('script');
            script.innerHTML = "ons.compile(document.getElementById('dialog')); console.log('script);";
            document.getElementById('dialog').appendChild(script);
         if (xmlHTTP.readyState == 4 && xmlHTTP.status == 200) 
            {//document.getElementById("diagcontent").innerHTML = "";
            var list = document.createElement('ons-list');
            var json = JSON.parse(xmlHTTP.responseText);
            console.log("addPresentation : " + json);
            for (var i=0;i<json.length;i++){
              var btn = document.createElement('ons-button');
              btn.setAttribute('modifier',"quiet");
              btn.innerHTML = json[i].name;
              var id = json[i].id_presentation
              var name = json[i].name;
                     btn.setAttribute('onclick',"addElement('""+id+"'",'""+name+"'");dialog.hide();");
              var listitem = document.createElement('ons-list-item');
              listitem.appendChild(btn);
              list.appendChild(listitem);
            }
            document.getElementById("diagcontent").appendChild(list);
            }
          }
        }
    }

ons.createDialog方法将创建一个对话框并将其附加到DOM。当你隐藏它时,它不会被破坏,你还需要调用dialog.destroy()

document.getElementById('diagcontent')将只返回ID为diagcontent的第一个项目,这就是为什么它只在第一次工作。

我认为你应该创建一次对话框,当按钮被点击时只使用dialog.show()方法。

你可以在每次点击按钮时创建它,但在这种情况下,你必须确保两个对话框不能同时出现在DOM中,因为你使用的是带有ID属性的元素,而ID应该是唯一的。