javascript代码在按钮单击时执行但不在输入按键时执行

时间:2013-03-04 09:42:52

标签: php javascript jquery

我正在搜索位置点击按钮。结果是onclick按钮。但是我提交了相同的代码提交功能,但这里搜索不起作用。

按钮点击代码。(这是有效的)

    $("#gobuttton").click(function() {
        $("#gmaplatlon").validate({
            rules:{"latitude":{number:true}, "longitude":{number:true}, "zoom":{digits:true,min:0}},
            errorPlacement:errorMessages
        });
        $("#address").change(function(){
            geocoder.geocode({"address": $(this).attr("value")}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { map.setZoom(16); map.setCenter(results[0].geometry.location); } else { alert("Geocode was not successful for the following reason: " + status); } });
        });
});



按下输入按钮代码(相同但不起作用)

     $('#frm').keypress(function(e) {
     var code = e.keyCode;
     if(code === 13){
         e.preventDefault();
         alert(code);
         $("#gmaplatlon").validate({rules:{"latitude":{number:true}, "longitude":{number:true}, "zoom":{digits:true,min:0}}, errorPlacement:errorMessages});
         $("#address").change(function(){ geocoder.geocode({"address": $(this).attr("value")}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { map.setZoom(16); map.setCenter(results[0].geometry.location); } else { alert("Geocode was not successful for the following reason: " + status); } }); });
       }
});


我的功能是 - 在文本框中输入地址并按回车键然后它应该在谷歌地图上搜索,这是在按钮点击时执行但不是按下回车键时执行。

HTML代码如下

<form method="post" name="frm" id="frm" enctype="multipart/form-data" action="<?php echo site_url('home/sendmapdata');?>"> 
 <input name="address" type="text" id="address" border: solid 1px; " size="60" value="Search from address" onClick="this.value=''"> 
<input type="button" id="gobuttton" value="go"> 
<div id="map_canvas" style="height: 480px; width:665px; " ></div> 
<input type="hidden" name="gmaplatlon" value="true"> 
<p align="center">
<input type="submit" name="okbutton" value="OK" >
</p>
</form>

4 个答案:

答案 0 :(得分:2)

Tyr this:

$(document).ready(function(){
    $('#address').keypress(function(e) {
         var code = e.keyCode;
         if(code === 13){
             //e.preventDefault();
             alert(code);
             $("#gmaplatlon").validate({rules:{"latitude":{number:true}, "longitude":{number:true}, "zoom":{digits:true,min:0}}, errorPlacement:errorMessages});
             $("#address").change(function(){ geocoder.geocode({"address": $(this).attr("value")}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { map.setZoom(16); map.setCenter(results[0].geometry.location); } else { alert("Geocode was not successful for the following reason: " + status); } }); });
         }
         return false;
    });
});

答案 1 :(得分:1)

尝试将此更改$('#frm').keypress(function(e)改为$('#address').keypress(function(e)

更新:

我认为你的问题是验证,它阻止调用map函数,在你的keypress函数中添加这个$('#frm').valid()

答案 2 :(得分:0)

您应该尝试将keypress事件发送到#address而不是#frm

$('#frm').on('keypress', '#address', function(e) {

答案 3 :(得分:0)

尝试在Google地图工作后添加e.preventDefault();

$(document).ready(function(){
    $('#address').keypress(function(e) {
         var code = e.keyCode;
         if(code === 13){
             //e.preventDefault();
             alert(code);
             $("#gmaplatlon").validate({rules:{"latitude":{number:true}, "longitude":{number:true}, "zoom":{digits:true,min:0}}, errorPlacement:errorMessages});
             $("#address").change(function(){ geocoder.geocode({"address": $(this).attr("value")}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { map.setZoom(16); map.setCenter(results[0].geometry.location); } else { alert("Geocode was not successful for the following reason: " + status); } }); });
         }
         e.preventDefault();
    });
});