将数据从id ='myid'发送到myFunction();

时间:2018-03-08 11:39:26

标签: javascript html

问题现在我将省名称发送到了setCamera();功能但没有任何反应。

<div class='fn'>
  <input id="ads" type="textbox" placeholder="Looking @">
  <a class="btnsearch" id="submit3"><i class="ion-ios-search"></i></a>
</div>

的javascript

document.getElementById('submit3').addEventListener('click', function() {
  setCamera();
});

function setCamera() {

  var address = document.getElementById('ads').value;

  geocoder.geocode({
    'ads': address
  }, function(results, status) {
    if (status == 'OK') {
      map.setCenter(results[0].geometry.location);
      map.setZoom(8);

    } else {
      alert('Geocode was not successful for the following reason: ' +
        status);
    }
  });
}

但是当我尝试这个时它会起作用。

<input id="address" type="textbox1" placeholder="ex.กทม" value="" 
   class="box1" >

的Javascript

    function codeAddress() {

        var address = document.getElementById('address').value;

        geocoder.geocode( { 'address': address}, function(results, status) {
          if (status == 'OK') {
            map.setCenter(results[0].geometry.location);
            map.setZoom(12);
            marker = new RichMarker({
              position: results[0].geometry.location,
              map: map,
              draggable: true,
              content: '<div class="my-marker">' +
              '<div class="dot"></div>' +
              '<div class="pulse"></div>' +
                '</div>'
              });

          } else {
            alert('Geocode was not successful for the following reason: ' + 
           status);
          }
        });
      }

我的setCamara()的任何原因:不工作?但我的代码地址似乎运作良好。

1 个答案:

答案 0 :(得分:1)

尝试在href中使用<a>

点击<a>时会触发事件。

请查看以下内容。

&#13;
&#13;
document.getElementById('submit3').addEventListener('click', function() {
alert("On click event is triggered..");
setCamera();
});

         function setCamera() {
alert("on func camera");
          var address = document.getElementById('ads').value;

          geocoder.geocode( { 'ads': address}, function(results, status) {
            if (status == 'OK') {
              map.setCenter(results[0].geometry.location);
              map.setZoom(8);

            } else {
              alert('Geocode was not successful for the following reason: ' 
            + status);
            }
          });
        }
&#13;
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div class='fn'>
   <input id="ads" type="textbox"  placeholder="Looking @" >
  <a href ="" class="btnsearch"  id="submit3"  ><i class="glyphicon glyphicon-search"></i></a>
   </div>
&#13;
&#13;
&#13;