.click() only firing once then no more after that unless i refresh the whole page and doesn't render at all if i call $(document).ready()

家住魔仙堡 提交于 2021-02-05 08:56:31

问题


I am trying to retrieve a quote and its author from a JSON file, the problem I am having is that once I click the button once, then the event is fired but only once and no more after that. Equally, when I call $(document).ready() the button doesn't fire at all.

I know that it is something to do with the way I am collecting the data from the JSON file (is an array of objects with text, author keys), and then subsequently calling the fetch when I want to retrieve a new quote with the correct author.

// https://type.fit/api/quotes //
// api link for quotes //
const button = document.getElementById('new-quote');
const baseUrl = 'https://type.fit/api/quotes';
let randomNumber = Math.floor(Math.random() * 100);

function getQuote() {
 fetch(baseUrl)
  fetch(baseUrl)
  .then(response => response.json())
  .then(quote => $('#text-output').text(quote[randomNumber].text))
   };

function getAuthor() {
  fetch(baseUrl)
  .then(response => response.json())
  .then(quote => $('#author').text(quote[randomNumber].author))
};

function renderQuoteToPage() {
  getQuote();
  getAuthor();
}


$('#new-quote').click(renderQuoteToPage);
$(document).ready(renderQuoteToPage);
body {
  width: 100%;
  height: auto;
}

#outer-wrapper {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="outer-wrapper" class="container-fluid">

<div id="quote-box" class="card w-50">
  <div class="card-body">

  <div class="row">
    <div class="col">
  <div id="text">
    <p id="text-output"></p>
      </div>
    </div>
  </div>
  
    <div class="row">
      <div class="col">
      <a href="#" id="tweet-quote">Tweet</a>
      </div>
      <div class="col">
      <div id="author">Author</div>
      </div>
  </div>
  
  <div class="row">
    <div class="col">
  <button id="new-quote">New quote</button>
    </div>
  </div>
  
  </div>
</div>
  
</div>

回答1:


You are calling the functions when you attach the handlers, so they are invoked without the appropriate handler being attached, and just once.

You need just the function definition:

$('#new-quote').click(renderQuoteToPage);
...
$(document).ready(renderQuoteOnPageLoad);

Edit: You also need to set randomNumber on each getQuote() call

// https://type.fit/api/quotes //
// api link for quotes //
const button = document.getElementById('new-quote');
const baseUrl = 'https://type.fit/api/quotes';
let randomNumber

function getQuote() {
  randomNumber = Math.floor(Math.random() * 100);
  fetch(baseUrl)
    .then(response => response.json())
    .then(quote => $('#text-output').text(quote[randomNumber].text))
};

function getAuthor() {
  fetch(baseUrl)
    .then(response => response.json())
    .then(quote => $('#author').text(quote[randomNumber].author))
};

function renderQuoteToPage() {
  getQuote();
  getAuthor();
}


$('#new-quote').click(renderQuoteToPage);
$(document).ready(renderQuoteToPage);
body {
  width: 100%;
  height: auto;
}

#outer-wrapper {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="outer-wrapper" class="container-fluid">

  <div id="quote-box" class="card w-50">
    <div class="card-body">

      <div class="row">
        <div class="col">
          <div id="text">
            <p id="text-output"></p>
          </div>
        </div>
      </div>

      <div class="row">
        <div class="col">
          <a href="#" id="tweet-quote">Tweet</a>
        </div>
        <div class="col">
          <div id="author">Author</div>
        </div>
      </div>

      <div class="row">
        <div class="col">
          <button id="new-quote">New quote</button>
        </div>
      </div>

    </div>
  </div>

</div>



回答2:


Few changes in your script and its done.

<script>
const button = document.getElementById('new-quote');
const baseUrl = 'https://type.fit/api/quotes';
let randomNumber;

function getQuote() {
  fetch(baseUrl)
  .then(response => response.json())
  .then(quote => $('#text-output').text(quote[randomNumber].text))
   };

function getAuthor() {
  fetch(baseUrl)
  .then(response => response.json())
  .then(quote => $('#author').text(quote[randomNumber].author))
};

function renderQuoteOnPageLoad() {
  randomNumber = Math.floor(Math.random() * 100);
  getQuote();
  getAuthor();
}

$(document).ready(function(){
    renderQuoteOnPageLoad();
});

 $(document).on("click","#new-quote",function(){
    renderQuoteOnPageLoad()
 })
</script>

Example Fiddle



来源:https://stackoverflow.com/questions/60682561/click-only-firing-once-then-no-more-after-that-unless-i-refresh-the-whole-pag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!