Javascript-尝试清空预标签会产生多个字符串

时间:2014-04-14 20:41:42

标签: javascript php html string pre

所以,当我只想接收温度数据时,我的脚本运行正常,但我决定也要显示位置。

$(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)"

1 个答案:

答案 0 :(得分:2)

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