Javascript-试图清空预标记会导致多个字符串


Javascript- Attemping to empty pre tags results multiple strings

因此,当我只想接收温度数据时,我的脚本运行良好,但我决定也要显示位置。

$(function() {
   $("#getzip").submit(function() {
    var zip_data = $(this).serialize();
    $.getJSON("get_weather.php", zip_data, function(data) {
       $("#temperature", "#location").empty();
   $("#location").append(JSON.stringify( data.current_observation.display_location.full, " "));
  $("#temperature").append(JSON.stringify( data.current_observation.temperature_string, " "));
  });
  return false;
 });
});

HTML

<h1>Weather</h1>
<hr />
<p>Enter your zipcode to recieve local weather.</p>
<form method="get" action="get_weather.php" id="getzip">
    <p>
    <label for="zip">ZIP:</label>
    <input type="text" name="zip" id="zip">
    <input type="submit" name="button" id="button" value="Submit" >
  </p>
</form>
  <pre id ="location">
  </pre>
  <pre id="temperature">
  </pre>
 </body>
</html>

不是在我再次点击提交后清空预标签,它只是生成

 "Toms River, NJ""Toms River, NJ""Toms River, NJ""Toms River, NJ""Toms River, NJ""Toms River, NJ"
"73.4 F (23.0 C)""73.4 F (23.0 C)""73.4 F (23.0 C)""73.4 F (23.0 C)""73.4 F (23.0 C)""73.4 F (23.0 C)"

您使用jquery选择器的方式不对。用$("#temperature, #location")替换$("#temperature", "#location"),它应该按预期工作