2012-10-10 10 views
31

क्लिक पर ईवेंट को आग लगाना और कुकीज़ सेट करना प्रतीत होता है लेकिन सबमिट करने के लिए एंटर दबाकर कुकीज़ सेट नहीं होती है और इसके बजाय पेज कुकीज़ के बिना रीडायरेक्ट करता है।Google के लिए place_changed ईवेंट को कैसे फायर करें एंटर कुंजी

function locationAuto() { 
     $('.search-location').focus(function() { 
     autocomplete = new google.maps.places.Autocomplete(this); 
     searchbox = this; 

     google.maps.event.addListener(autocomplete, 'place_changed', function() { 
      var thisplace = autocomplete.getPlace(); 
      if (thisplace.geometry.location != null) { 
       $.cookie.raw = true; 
       $.cookie('location', searchbox.value, { expires: 1 }); 
       $.cookie('geo', thisplace.geometry.location, { expires: 1 }); 
      } 
     }); 
}); 

.search-location एकाधिक टेक्स्टबॉक्स पर एक वर्ग है। एक सबमिट बटन है जो कुकीज़ और रीडायरेक्ट (सर्वर साइड) से मूल्य लेता है

+0

यह Google Mapa API प्रश्न नहीं है। – Marcelo

+12

इसे Google मानचित्र एपीआई v3 प्लेस लाइब्रेरी –

+0

में प्रश्न यह है कि यह एक डीओएम तत्व पर एक घटना को फायर करने के बारे में है, न कि Google मानचित्र API के बारे में। – Marcelo

उत्तर

35

जोनाथन Caulfield के जवाब से अनुकूलित:

$('.search-location').keypress(function(e) { 
    if (e.which == 13) { 
    google.maps.event.trigger(autocomplete, 'place_changed'); 
    return false; 
    } 
}); 
+0

अच्छा जवाब। मैं जो कुछ चाहता था उसे करने के लिए इसे आसानी से अनुकूलित करने में सक्षम था, जो स्वत: पूर्ण श्रोता को आग लगा दी गई थी और फिर आगे बढ़कर फॉर्म जमा कर दी गई थी। –

+5

यह थोड़ी देर हो गया है क्योंकि इसका उत्तर दिया गया था, इसलिए शायद Google के स्वत: पूर्ण विजेट में कुछ बदल गया है, लेकिन यदि मैं इस कोड का उपयोग करता हूं तो 'स्वतः पूर्ण' ऑब्जेक्ट पर 'getPlace() 'विधि शून्य हो जाएगी। मानक 'place_changed' हैंडलर को कीप्रेस पर निकाल दिया जाता है, इसलिए यहां से झूठी वापसी करना पर्याप्त है (मेरे लिए) जिसमें फॉर्म को सबमिट करने से रोकना है। –

1

शायद सबसे उपयोगकर्ता के अनुकूल समाधान नहीं है लेकिन आप एंटर कुंजी प्रेस को अक्षम करने के लिए JQuery का उपयोग कर सकते हैं।

इस तरह कुछ ...

$('.search-location').keypress(function(e) { 
    if (e.which == 13) { 
    return false; 
    } 
}); 
+2

मैंने कोड की कोशिश की लेकिन लगता है कि ऑटो-पूर्ण फोकस है जब टेक्स्ट को टेक्स्टबॉक्स के बजाए दबाया जाता है। –

+1

प्रश्न का उत्तर नहीं देता है, और कुछ ऐसा करने का सुझाव देता है जो बहुत खराब यूएक्स है। –

+0

इसका उपयोग स्वीकृत उत्तर के रूप में किया और अनुकूलित किया गया था। –

5

ऊपर प्रतिक्रियाओं के दोनों एक सवाल फायरिंग के सामान्य प्रश्न के लिए अच्छा जवाब हैं जब उपयोगकर्ता प्रेस "दर्ज।" हालांकि - Google Places Autocomplete का उपयोग करते समय मैं एक और अधिक विशिष्ट समस्या में भाग गया, जो ओपी की समस्या का हिस्सा हो सकता है। Place_changed ईवेंट को कुछ भी उपयोगी करने के लिए, उपयोगकर्ता को स्वत: पूर्ण विकल्पों में से एक को चुनने की आवश्यकता है। यदि आप बस 'place_changed' को ट्रिगर करते हैं, तो if() ब्लॉक छोड़ दिया जाता है और कुकी सेट नहीं होती है।

वहाँ सवाल यहाँ के दूसरे भाग के लिए एक बहुत अच्छा जवाब है: https://stackoverflow.com/a/11703018/1314762

नोट: amirnissim का जवाब, नहीं चुना जवाब है, कारणों से आप अगर आप अधिक है में चलाने के लिए उपयोग करने के लिए एक है एक ही पृष्ठ पर एक स्वत: पूर्ण इनपुट से।

4

मुझे इस समस्या का भी सामना करना पड़ा है, और एक अच्छा समाधान के साथ आया। मेरी वेबसाइट में मैं जमा करने से पहले एक छिपी हुई इनपुट में autocomplete.getPlace()। Formatted_address को सहेजना चाहता था। फ़ॉर्म के सबमिट बटन पर क्लिक करते समय यह अपेक्षित काम करता था, लेकिन स्वत: पूर्ण ड्रॉपडाउन मेनू में चयन पर एंटर कुंजी दबाते समय नहीं। मेरा समाधान निम्नानुसार था:

$(document).ready(function() { 

    // Empty the value on page load 
    $("#formattedAddress").val(""); 
    // variable to indicate whether or not enter has been pressed on the input 
    var enterPressedInForm = false; 

    var input = document.getElementById("inputName"); 
    var options = { 
     componentRestrictions: {country: 'uk'} 
    }; 
    autocomplete = new google.maps.places.Autocomplete(input, options); 

    $("#formName").submit(function(e) { 
     // Only submit the form if information has been stored in our hidden input 
     return $("#formattedAddress").val().length > 0; 
    }); 

    $("#inputName").bind("keypress", function(e) { 
     if(e.keyCode == 13) { 
      // Note that simply triggering the 'place_changed' event in here would not suffice, as this would just create an object with the name as typed in the input field, and no other information, as that has still not been retrieved at this point. 

      // We change this variable to indicate that enter has been pressed in our input field 
      enterPressedInForm = true; 
     } 
    }); 

    // This event seems to fire twice when pressing enter on a search result. The first time getPlace() is undefined, and the next time it has the data. This is why the following logic has been added. 
    google.maps.event.addListener(autocomplete, 'place_changed', function() { 
     // If getPlace() is not undefined (so if it exists), store the formatted_address (or whatever data is relevant to you) in the hidden input. 
     if(autocomplete.getPlace() !== undefined) { 
      $("#formattedAddress").val(autocomplete.getPlace().formatted_address); 
     } 
     // If enter has been pressed, submit the form. 
     if(enterPressedInForm) { 
      $("#formName").submit(); 
     } 
    }); 
}); 

यह समाधान अच्छी तरह से काम करता प्रतीत होता है।

संबंधित मुद्दे