简体   繁体   English

单击按钮刷新页面div

[英]Refresh page div on button click

I'm building a phonegap application using jquery mobile. 我正在使用jquery mobile构建一个phonegap应用程序。 I have one page div that displays similar data for each of 10 different steps. 我有一个页面div,它为10个不同的步骤分别显示相似的数据。 My data is being read in from JSON. 我的数据正在从JSON中读取。 I can correctly incrament the steps by clicking a button, and display this info by using pagebefore show (if I move off the page and then back to it). 我可以通过单击一个按钮来正确增加步骤,并通过使用showbefore show(如果我先移出页面然后返回页面)来显示此信息。 But I want the button to refresh (reload) the current page with the new info when I click it. 但是我希望按钮在单击时用新信息刷新(重新加载)当前页面。 I can't seem to get it working. 我似乎无法正常工作。 I've tried location.reload() and some others with no avail. 我试过location.reload()和其他一些都没有用。 Can someone help out a newbie here? 有人可以在这里帮新手吗?

Javascript: 使用Javascript:

var currentStep = 0;  

$.getJSON("rubric.json", function(data) {
    jsonRubric = data;

});

$("#nextStep").on('click', function() {
if (currentStep <9){
currentStep += 1;
location.reload(true);}
});

function setRubricInfo() {
    $('#rubricTitle').html(jsonRubric.rubric[currentStep].title);
    $('#criteria').html(jsonRubric.rubric[currentStep].criteria);
     $('#meets').html(jsonRubric.rubric[currentStep].meets);
     $('#dnmeet').html(jsonRubric.rubric[currentStep].dnmeet);
     $('#exceeds').html(jsonRubric.rubric[currentStep].exceeds);
}
$("#stepOneRubric").on("pagebeforeshow", function(){
setRubricInfo();
});

"#nextStep" is my button, "#StepOneRubric" is my page div. "#nextStep"是我的按钮, "#StepOneRubric"是我的页面div。 All the changing of the content happens on pagebeforeshow currently. 内容的所有更改均发生在当前页面上。

HTML: HTML:

<!-- Step 1 Rubric/page14 -->
<div id="stepOneRubric" data-role="page">
    <div data-role="header" data-theme="a"> <a class="ui-btn-left" href="#eightStepHome" data-role="button" data-iconpos="notext"
          data-icon="arrow-l"> Button </a>
    <h3> Step One Rubric </h3>
  </div>
    <div data-role="content">

  <div align="center">  
  <h4 id="rubricTitle"> sfasfd </h4>
</div>

   <div id=rubricCollapsible data-role="collapsible-set">
        <div data-role="collapsible" data-collapsed="true">
        <h3> Criteria </h3>
      <p id="criteria"> Critera text</p>
      </div>
        <div data-role="collapsible" data-collapsed="true">
        <h3> Does Not Meet </h3>
        <p id="dnmeet"> Critera text</p>
      </div>
        <div data-role="collapsible" data-collapsed="true">
        <h3> Meets </h3>
        <p id="meets"> Critera text</p>
      </div>
        <div data-role="collapsible" data-collapsed="true">
        <h3> Exceeds </h3>
        <p id="exceeds"> Critera text       
        <div data-role="controlgroup" data-type="horizontal">
                </div>
        </div>

      </div>
  </div>
         <a href="#stepOneRubric" id= "nextStep" data-role="button" data-theme="b" > Next 
         </a>
          <div id = "nextStep" a href = "#stepOneRubric" data-role="button" data-theme="b" > Next 
         </div>
      </div>

Use .trigger('create') after appending new items into [data-role=page] . 将新项目附加到[data-role=page]后,使用.trigger('create') This will re-enhance the markup of your code as you're using .html() . 当您使用.html()这将重新增强代码的标记。

There is no need to refresh or reload the page. 无需refresh或重新加载页面。 However, if you want to give the feeling that the page is being refreshed, you can reload the same page using $.mobile.changePage() with option allowSamePageTransition: true . 但是,如果您想感觉到页面正在刷新,则可以使用$.mobile.changePage()allowSamePageTransition: true选项重新加载同一页面。

Check this example. 检查此示例。

var currentStep = 0;
$("#nextStep").on('click', function() {
 if (currentStep <9){
  currentStep++;
  setRubricInfo();
 }
 $('[data-role=page]').trigger('create');
});

What version of jQuery? 什么版本的jQuery? .live() is deprecated. .live()已弃用。

"As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live()." “从jQuery 1.7开始,不推荐使用.live()方法。请使用.on()附加事件处理程序。jQuery较早版本的用户应优先使用.delegate()而不是.live()。”

http://api.jquery.com/live/ http://api.jquery.com/live/

Use .on() instead. 使用.on()代替。

$("#stepOneRubic").on('click', '#nextStep', function() {
    if (currentStep <9){
        currentStep += 1;}
    location.reload();
});

http://api.jquery.com/on/ http://api.jquery.com/on/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM