سؤال

وأنا أحاول أن تجعل شكل الإكمال التلقائي باستخدام مسج، ولكني واجهت مشكلة. أحاول أن تجعل من ذلك أنه عندما يبدأ المستخدم في كتابة اسم المكان الذي هو في قاعدة البيانات الخاصة بنا، وتبين لهم الاسم الكامل للمكان في الإكمال التلقائي، ولكن عند النقر على ذلك، يحصل شغل النص مع عنوان المكان الذي يمكن أن يكون موجودا مع خرائط جوجل. ما هي أفضل طريقة لتحقيق ذلك؟

هل كانت مفيدة؟

المحلول

وهنا رسم تقريبي جدا من بعض التعليمات البرمجية التي سوف تحصل على انك بدأته. بأي حال من الأحوال هو كاملة، ولكن يجب أن تعطيك نقطة انطلاق جيدة. نأمل أن يساعد

$("#mySearchBox").change(function(){
/*
    send a ajax request to receive a json object.
    We use the callback function to populate the 
    a div tag "autocomplete" with the names
*/
$.getJSON("http://myurl.com/myPage?search="+$(this).val()
        function(data){
          $.each(data.items, function(i,item){
            /*
                Assume item looks like this

                item = {[{"address": 100 Main st",
                           "name": "Bob and Joe's Dinner"],
                            ...
                       }
            */              
                /* Populate autocomplete with new spans
                   We use the text attribute to hold the address
                   You may want to look in to jquery data() feature. 
                */
                $("#autoComplete").append('<span text="'+ item.address +
                                          '" class="searchResult">'+ item.name +'</span>');
          });
        });

و})؛

$("#autoComplete .searchResults").click(function(){
    /*  
        Here we handle what the user clicks on.
        Pull out the address from the attr and 
        put that back in the mySearchBox
    */
    var address = $(this).attr("text");

    //Load the adress back int the textfield
    $("#mySearchBox").val = address;
});

نصائح أخرى

وتمرير ربما في كامل الجسم "الشخص" في جافا سكريبت باستخدام كائن JSON. ثم يمكنك انتقاء واختيار المكان الذي تريد كل قطعة من وجوه للذهاب.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top