【问题标题】:JavaScript/jQuery: Clearing data after an API callJavaScript/jQuery:API 调用后清除数据
【发布时间】:2019-03-17 21:17:06
【问题描述】:

我正在开发一个简单的 JS 项目,它从 API 调用数据并返回到页面。还有一个“重置”按钮,可以将 API 调用重置回基线。我无法弄清楚如何让元素重置。通常我会用 CSS 类来做,但是这一次,我将构建一个更大的分配,它将从用户 ID 调用 API。所以我想纯粹通过 JS/jQuery 来做这件事,而不使用表单元素。

HTML

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Weather Vain</title>
</head>
<h1> Weather 4 Ever </h1>
<body class = "change">
  <p class="prompt">Type in a US city to get temperature data!</p>
  <form class="pure-form">
    <input type="text" class="pure-input-rounded">
    <button type="submit" class="pure-button">Search</button>
    <button type="submit" class="reset-button">Reset</button>
  </form>

  <p id="forecast"></p>

  <p class="date"></p>
</body>
</html>

CSS

html, body {
  background-color: #5CBF94;
  font-weight: 100;
  color: #FFF;
}

h1, h2, h3, h4, h5, h6, p {
  text-align: center;
  font-weight: 100;
  width: 100%;
}

h1 {
  font-size: 3em;
}

.prompt {
  font-size: 1.2em;
  margin-top: 4em;
}

form {
  color: #000;
  width: 290px;
  margin: 0 auto;
}

.date {
  position: absolute;
  bottom: 0;
  text-align: center;
}

img {
  margin: 0 auto;
}

JS

console.log("Script loaded")
"use strict";

(function() {
    $('.pure-button').click(function(e) {
        e.preventDefault()
        console.log("click noticed")
    $.ajax({
        url: "https://api.openweathermap.org/data/2.5/weather?q=" + $('.pure-input-rounded').val() + ",US&appid=6549863730776a52fadf3fe935d5eecc&units=Imperial",
        type: "GET",
        success: function(data){
            var temp = data.main.temp
            var sky = data.weather[0].description
            $('#forecast').text("The current temperature in " + $('.pure-input-rounded').val() + " is " + temp + " degrees Fahrenheit. It is currently " + sky + "."  )

        }

    })
})

  (".reset-button").onclick.reset() //this is the part that I need to figure out.

})();

CodePen 链接:https://codepen.io/anfperez/pen/NJzPoE

谁能给我指点?

【问题讨论】:

  • 呼叫重置? $("form")[0].reset() 或者使用重置按钮type="reset"
  • 你最好使用$('.pure-form').submit(function(e) {,看我的回答如下
  • $('.pure-form').on('reset', function(e) {清除其他部分

标签: javascript jquery json onclick reset


【解决方案1】:

为了去除输入值,你可以在Jquery中使用.val函数并给它一个参数''

此外,为了提高性能,将 Jquery 元素存储在变量中是一种更好的做法,这样您就不必在每次执行操作(如单击)时都使用 Jquery 来查找它们

console.log("Script loaded")
"use strict";

(function() {
  var $inputElement = $('.pure-input-rounded');
  var $forecastElement = $('#forecast');

  $('.pure-button').click(function(e) {
    e.preventDefault()
    console.log("click noticed")
    $.ajax({
      // var city = $('.pure-input-rounded').val()
      url: "https://api.openweathermap.org/data/2.5/weather?q=" + $inputElement.val() + ",US&appid=6549863730776a52fadf3fe935d5eecc&units=Imperial",
      type: "GET",
      success: function(data) {
        console.log(data)
        var temp = data.main.temp
        var sky = data.weather[0].description
        $forecastElement.text("The current temperature in " + $('.pure-input-rounded').val() + " is " + temp + " degrees Fahrenheit. It is currently " + sky + ".")

      }

    })
  })

  $(".reset-button").click(function() {
    $inputElement.val('');
  })

})();
html,
body {
  background-color: #5CBF94;
  font-weight: 100;
  color: #FFF;
}

h1,
h2,
h3,
h4,
h5,
h6,
p {
  text-align: center;
  font-weight: 100;
  width: 100%;
}

h1 {
  font-size: 3em;
}

.prompt {
  font-size: 1.2em;
  margin-top: 4em;
}

form {
  color: #000;
  width: 290px;
  margin: 0 auto;
}

.date {
  position: absolute;
  bottom: 0;
  text-align: center;
}

img {
  margin: 0 auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Weather Vain</title>
</head>
<h1> Weather 4 Ever </h1>

<body class="change">
  <p class="prompt">Type in a US city to get temperature data!</p>
  <form class="pure-form" onsubmit="event.preventDefault();">
    <input type="text" class="pure-input-rounded">
    <button type="submit" class="pure-button">Search</button>
    <button type="button" class="reset-button">Reset</button>
  </form>

  <p id="forecast"></p>

  <p class="date"></p>
</body>

</html>

【讨论】:

    【解决方案2】:

    无需编写任何代码。

    你使用了一个表单,所以你只需要使用一个重置按钮。

    这是一个基本的 HTML 1 功能:

    &lt;button type="reset" class="pure-button"&gt;Reset&lt;/button&gt;

    (function() {
    
    $('.pure-form').submit(function(e) {
      e.preventDefault()
      console.log("click noticed")
    	$.ajax({
    		// var city = $('.pure-input-rounded').val()
    		url: "https://api.openweathermap.org/data/2.5/weather?q=" + $('.pure-input-rounded').val() + ",US&appid=6549863730776a52fadf3fe935d5eecc&units=Imperial",
    		type: "GET",
    		success: function(data){
    			console.log(data)
    
          $('#forecast').text(`The current temperature in ${data.name} is ${data.main.temp} degrees Fahrenheit. It is currently ${data.weather[0].description}`)
    		}
    
    	})
    })
    
    $('.pure-form').on('reset', function(e) {
      $('#forecast').text('');
    })
    
    })();
    
    // this is useless => (".reset-button").onclick("reset") 
    html, body {
      background-color: #5CBF94;
      font-weight: 100;
      color: #FFF;
    }
    h1, h2, h3, h4, h5, h6, p {
      text-align: center;
      font-weight: 100;
      width: 100%;
    }
    h1 {
      font-size: 3em;
    }
    .prompt {
      font-size: 1.2em;
      margin-top: 4em;
    }
    form {
      color: #000;
      width: 290px;
      margin: 0 auto;
    }
    .date {
      position: absolute;
      bottom: 0;
      text-align: center;
    }
    img {
      margin: 0 auto;
    }
    
    input { background-color: #FFF }
    <link rel="stylesheet" href="https://unpkg.com/purecss@1.0.0/build/pure-min.css" integrity="sha384-nn4HPE8lTHyVtfCBi5yW9d20FjT8BJwUXyWZT9InLYax14RDjBj46LmSztkmNP9w" crossorigin="anonymous">
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    
    <h1> Weather 4 Ever </h1>
    <p class="prompt">Type in a US city to get temperature data!</p>
    <form class="pure-form">
      <fieldset>
        <input id="input-city" type="text" class="pure-input-rounded"   placeholder="Enter city name here..." /> 
    
      </fieldset>
      <button type="submit" class="pure-button">Search</button>
      <button type="reset" class="pure-button">Reset</button>
    </form>
    
    <p id="forecast"></p>

    【讨论】:

      猜你喜欢
      • 2015-10-18
      • 2013-01-09
      • 2021-05-07
      • 2016-07-22
      • 2020-06-02
      • 1970-01-01
      • 2019-09-29
      • 1970-01-01
      • 2021-08-17
      相关资源
      最近更新 更多