【问题标题】:Javascript two functions to be executed on one click but after getting values from first functionJavascript 两个函数将在一次单击时执行,但在从第一个函数获取值之后
【发布时间】:2017-05-17 08:26:36
【问题描述】:

我在为地理位置和 AJAX 表单提交创建 javascript 逻辑方面需要帮助。 我在我的网站上进行了搜索,其中有一个附近的搜索按钮。这里的问题是我想通过 AJAX 提交关键字,但同时我想从用户那里获取地理位置并发送纬度和经度值以及表单,但作为我按下搜索附近按钮,这两个功能同时执行(直接提交表单,浏览器提示用户允许定位,但提交的表单没有 lat 和 long 值)。 所以请帮助我实现逻辑,以便浏览器首先获取当前位置值,然后将它们与用户输入的关键字一起提交,我也想一键执行这些功能,但不能同时执行。 下面是我的js和form代码。 希望我清楚。 提前致谢

    <form method="post" class="form-inline" onsubmit="download(this['searchQuery'].value, this['Latitude'].value)" id="simple-firm" action="#">
    <div class="form-group">
      <div id="divSample" class="hideClass">Latitude:
        <input type="text" id="Latitude" name="Latitude" value="">
        <br>
        <br>Longitude:
        <input type="text" id="Longitude" name="Longitude" value="">
        <br>
        <input type="text" id="Position1" name="Position1" value="Miami">
      </div>
      <input type="text" class="form-control search-term" placeholder="Keyword"  name="searchQuery" id="searchQuery"> 
      <input onclick="showLocation();" type="submit" class="search-btn" value="Search" id="simple-submit-button">
     </div>
   </form>

JAvascript for geo location using google APIs:

    function showLocation() {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(onGeoSuccess, onGeoError);
            } else {
                alert("Your browser or device doesn't support Geolocation");
            }
        }

        // If we have a successful location update
        var lat;
        function onGeoSuccess(event) {
            document.getElementById("Latitude").value = event.coords.latitude;
            document.getElementById("Longitude").value = event.coords.longitude;
            lat  = event.coords.latitude;
            document.getElementById("Position1").value = event.coords.latitude + ", " + event.coords.longitude;
            console.log(document.getElementById("Latitude").value);
        }

        // If something has gone wrong with the geolocation request
        function onGeoError(event) {
            alert("Error code " + event.code + ". " + event.message);
        }


 AJAX:


// AJAX for Simple Form

         $("#simple-submit-button").click(submitform);

function handleResponse(responseObj) {
    console.log("response is "+responseObj.status+" and "+responseObj.text);
    if( responseObj.status == "ok" ) {
       $("#response_element").html(responseObj.text).css("color","green");
    } else {
       $("#response_element").html(responseObj.text).css("color","red");
    }
}


    function submitform() {
        var check = document.getElementById('Latitude').value;
         if(check == ""){
            alert('danger');
        } else{
        var where = $("#simple-firm").attr("action");
        var fi = $("#simple-firm[name=Latitude]").val();
        var fj = $("#simple-firm[name=Longitude]").val();
        var gk = $("#simple-firm[name=searchQuery]").val();
        var what = {Latitude: fi, Longitude: fj, searchQuery: gk, lat};
        $.post( where, what, handleResponse, "json");
        alert('success');
    }
    }

【问题讨论】:

    标签: javascript jquery ajax forms google-geolocation


    【解决方案1】:

    你应该从第一个函数调用第二个函数

    【讨论】:

    • 感谢您的回复。请您再解释一下您的答案吗?
    • @ARKhan 不是在点击时运行这两个函数,而是只运行第一个函数,并在函数末尾获取位置等,(在其中)调用提交表单的第二个函数。当然,这只是基本的;你会想要错误处理等。
    猜你喜欢
    • 2014-07-14
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多