为什么URL包含HTML表单中的数据?

时间:2011-11-16 07:46:28

标签: javascript html drupal

在我在Drupal的一个页面上,我有一个包含以下html代码的面板:

<head>    
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?libraries=geometry&sensor=false"></script>

    <script type="text/javascript">
       var  dirService, initAddr, endLoc, doc;
       function initialize() {
        dirService = new google.maps.DirectionsService();
         initAddr = "85 E San Fernando Street San Jose, CA 95113";
       }

    function showLocation() {
        doc = document.getElementById("results_area");

        endLoc = document.forms[0].address2.value;
        findFromAddress();
    }


       function findFromAddress() {
         dirService.route({'destination': initAddr, 'origin': endLoc, 'travelMode': google.maps.TravelMode.DRIVING}, function (result, status) {
          if (status === google.maps.DirectionsStatus.OK) {
        var distance = result.routes[0].legs[0].distance.value;
                var miles = Math.round(distance * 0.000621371192 * 100) / 100;
            alert("Distance from Loves Cupcakes is roughly" + miles
                 + " miles" + "Estimated price is (price)" );
          }
          else {
            alert("INVALID ZIP CODE");
          }
        });
    }

    </script>
</head>

<body onload="initialize()">
<p> Enter in zip code of desired delivery location</p>
<form action="#" onsubmit="showLocation(); return false;">
    <p><input class="address_input" name="address2" size="20" type="text"  /> <input name="find" type="submit" value="Search" /></p>
</form>
<p id="results_area">&nbsp;</p>

简单地说,它需要一个zipcode并计算距指定位置的距离。

HTML页面可以自行运行,但是当我将其输入到面板中时,会发生一些我不理解的奇怪的事情。当我点击“提交”按钮时,页面会重新加载,但网址略有不同,并且不会弹出警告框。 URL从../contact_us更改为../Contact_Us?address2=&find=Search#。我理解address2和搜索是我的HTML代码中的元素,但任何人都可以帮我弄清楚为什么会发生这种情况(我假设它与drupal有关,而不是代码本身,但不太确定)?

1 个答案:

答案 0 :(得分:0)

单击提交按钮时,浏览器会向地址2发送HTTP GET请求并查找值作为参数。

在您的情况下,address2没有值,find的值为'Search'

请点击此处查看详细说明:http://tools.ietf.org/html/rfc3986#section-3