【问题标题】:input text doesn't save value on pressing enter输入文本在按 Enter 时不保存值
【发布时间】:2014-02-07 06:34:20
【问题描述】:

我有一个文本字段,用户应该在其中输入地址。单击搜索按钮后,将执行一个函数,该函数使用该地址查找附近的 POI。这运作良好。但是,当用户点击返回键时,文本字段的值不会保存在变量中。这会导致使用地址变量的旧值执行搜索。

说实话,我什至不明白为什么它会有所不同。

HTML

<form action="" method="post">
<input type="text" class="innenfeld" id="addressInput" name="addressInput">
<input type="button" class="send" onclick="newsearch()"/>
</form>

JavaScript

   function newsearch() {
     var address = document.getElementById("addressInput").value;
     var geocoder = new google.maps.Geocoder();
     geocoder.geocode({address: address}, function(results, status) {
       if (status == google.maps.GeocoderStatus.OK) {
        searchLocationsNear(results[0].geometry.location);
       } else {
         alert(address + ' not found');
       }
     });
   }

【问题讨论】:

    标签: javascript html textfield


    【解决方案1】:

    点击 Enter 正在提交表单。您需要添加一个调用您的函数的 onsubmit 事件(返回 false 会阻止它实际离开页面)

    <form action="" method="post" onsubmit="newsearch(); return false">
    

    【讨论】:

    • 这样,他的按钮可以写成submit类型而没有onclick事件像&lt;input type="submit" class="send" /&gt;
    猜你喜欢
    • 2013-11-20
    • 2011-10-03
    • 2016-12-04
    • 2017-04-29
    • 2015-02-17
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    • 2021-12-20
    相关资源
    最近更新 更多