简体   繁体   English

第一次单击按钮后,对话框的动态内容仅显示一次

[英]dialog'dynamic content shows only once after first button click

I am trying to show a list into a dialog after a button click, the content of the list is retrieved from the server, my list shows when i click the button for the first time but then the dialog is shown empty. 单击按钮后,我试图在对话框中显示列表,从服务器检索列表的内容,当我第一次单击按钮时,列表显示,但是对话框显示为空。

the code of onclick is working an addElement runs every time the button is clicked. onclick的代码正在工作,每次单击按钮时都会运行addElement。

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

I even tried doing this way : 我什至尝试这样做:

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

      "
      > 

Again it gives the same result. 再次给出相同的结果。

Js method : 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);

            }
          }
        }
    }

The ons.createDialog method will create a dialog and append it to the DOM. ons.createDialog方法将创建一个对话框并将其附加到DOM。 It will not be destroyed when you hide it, you also need to call dialog.destroy() . 隐藏它时不会销毁它,还需要调用dialog.destroy()

document.getElementById('diagcontent') will only return the first item with the ID diagcontent that's why it only works the first time. document.getElementById('diagcontent')将仅返回ID为diagcontent的第一项,这就是为什么它仅在第一时间有效。

I think you should create the dialog once and just use the dialog.show() method when the button is clicked. 我认为您应该一次创建对话框,并且在单击按钮时仅使用dialog.show()方法。

You can create it every time the button is clicked but in that case you must be sure that two dialogs cannot be present in the DOM at the same time since you're using an element with an ID property and IDs are supposed to be unique. 您可以在每次单击按钮时创建它,但是在这种情况下,必须确保不能同时在DOM中出现两个对话框,因为您使用的是具有ID属性的元素,并且ID应该是唯一的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的显示“上一个”内容的按钮的JS代码只有在我单击“下一个”按钮两次后才能工作 - My JS code for the button that shows 'previous' content only works after I click the 'next' button twice 如何在按钮单击上添加动态选项卡,但仅一次 - how to add dynamic tabs on button click but only once 单击按钮后,文本区域的内容应显示到另一个div或文本区域。 单击后,第一个文本区域应清除。 - Once click the button the content of the textarea should be display to another div or textarea. and first textarea should be clear after clicking. Angular应用仅在刷新后显示调整后的动态内容 - Angular app only shows adjusted dynamic content after refresh 首次单击Mapbox Map后,GeoJson LineString仅加载一次 - GeoJson LineString loading only once after first click in Mapbox Map JavaScript单击按钮仅一次 - Javascript click button only once 按钮单击仅适用一次 - Button click only works once 当单击按钮时,while循环仅显示数据库中的最后一行[以显示模式对话框] - while loop shows only the last row in database when click at button[to display modal dialog] 单击一次后禁用按钮 - Disabling the button after once click 带有jQuery的动态div,显示点击内容 - Dynamic div with jQuery that shows content on click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM