Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [jQuery] Autocomplete dla kilku inputów z taką samą klasą.
lustfingers
post 30.03.2017, 17:27:42
Post #1





Grupa: Zarejestrowani
Postów: 99
Pomógł: 5
Dołączył: 18.03.2015

Ostrzeżenie: (0%)
-----


Próbuje wdrożyć ten autocomplete: http://www.bewebdeveloper.com/tutorial-abo...ysql-and-jquery i działa ładnie z tym że posiadam naście identycznych inputów a autocomplete pojawia się tylko dla pierwszego.

Mój input wygląda tak:

  1. <li>
  2. <input type="text" class="ingredient" name="ingredient[]" onkeyup="autocomplet()" autocomplete="off" value="" />
  3. <ul id="ingredient_list_id"></ul>
  4. </li>


a kolejne inputy pojawiają się przy użyciu funkcji clone(), natomiast kod js prezentuje się tak:

  1. function autocomplet() {
  2. var min_length = 1; // min caracters to display the autocomplete
  3. var keyword = $('.ingredient').val();
  4. if (keyword.length >= min_length) {
  5. $.ajax({
  6. url: 'ajax_refresh.php',
  7. type: 'POST',
  8. data: {keyword:keyword},
  9. success:function(data){
  10. $('#ingredient_list_id').show();
  11. $('#ingredient_list_id').html(data);
  12. }
  13. });
  14. } else {
  15. $('#ingredient_list_id').hide();
  16. }
  17. }
  18.  
  19. // set_item : this function will be executed when we select an item
  20. function set_item(item) {
  21. // change input value
  22. $('.ingredient').val(item);
  23. // hide proposition list
  24. $('#ingredient_list_id').hide();
  25. }


więc jest minimalnie inny od tego z przykładu ponieważ w przykładzie opierał się na ID. Wiem że muszę dla każdego inputa nadać inne ID i dostosować do tego kod js tylko nie mam zielonego pojęcia jak, od czego zacząć?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
lustfingers
post 3.04.2017, 17:09:18
Post #2





Grupa: Zarejestrowani
Postów: 99
Pomógł: 5
Dołączył: 18.03.2015

Ostrzeżenie: (0%)
-----


Znowu nie rozumiem o co chodzi i tu chyba mam największy problem biggrin.gif przydałyby się nieco konkretniejsze wskazówki, coś co pobudzi do myślenia na razie nie wiem w którą stronę iść mimo że problem wydaje się być bardzo podobny do ostatniego nie mam pomysłu jak to ogarnąć.


EDIT.
Ponieważ w js mam już podobna działającą funkcję datepickera próbowałem na podobnej zasadzie dopisać obsługę autocomplete, niestety rezultat nie przyniósł chcianych efektów.

W chwili obecnej to mam:

  1. $('button').on('click', function(event) {
  2. event.preventDefault();
  3. var lines = $('#lines');
  4. var i = 0;
  5. $('input.datepicker').datepicker("destroy");
  6. lines.append(lines.find('li:first-child').clone());
  7. $('.datepicker').each(function () {
  8. $(this).attr("id",'datepicker' + i).datepicker();
  9. i++;
  10. });
  11. });
  12. $('input.datepicker').datepicker();
  13. $( function() {
  14. $( "#datepicker" ).datepicker();
  15. } );
  16.  
  17. // autocomplet : this function will be executed every time we change the text
  18. function autocomplet() {
  19. var min_length = 1; // min caracters to display the autocomplete
  20. var keyword = $('.ingredient').val();
  21. if (keyword.length >= min_length) {
  22. $.ajax({
  23. url: 'ajax_refresh.php',
  24. type: 'POST',
  25. data: {keyword:keyword},
  26. success:function(data){
  27. $('#ingredient_list_id').show();
  28. $('#ingredient_list_id').html(data);
  29. }
  30. });
  31. } else {
  32. $('#ingredient_list_id').hide();
  33. }
  34. }
  35.  
  36. // set_item : this function will be executed when we select an item
  37. function set_item(item) {
  38. // change input value
  39. $('.ingredient').val(item);
  40. // hide proposition list
  41. $('#ingredient_list_id').hide();
  42. }
  43.  


Na próbę jak zrobię tak:

Kod
<li>
     <input type="text" class="ingredient" name="ingredient[]" onkeyup="autocomplet()" autocomplete="off" value=""  />
     <ul id="ingredient_list_id"></ul>
     <input type="text" class="datepicker" name="deliverydate[]" autocomplete="off" value=""  />
</li>
<li>
     <input type="text" class="ingredient" name="ingredient[]" onkeyup="autocomplet2()" autocomplete="off" value=""  />
     <ul id="ingredient_list_id"></ul>
     <input type="text" class="datepicker" name="deliverydate[]" autocomplete="off" value=""  />
</li>


i

Kod
$('button').on('click', function(event) {
event.preventDefault();
var lines = $('#lines');
var i = 0;
$('input.datepicker').datepicker("destroy");
lines.append(lines.find('li:first-child').clone());
$('.datepicker').each(function () {
$(this).attr("id",'datepicker' + i).datepicker();
i++;
});
});
$('input.datepicker').datepicker();
$( function() {
$( "#datepicker" ).datepicker();
} );

   // autocomplet : this function will be executed every time we change the text
function autocomplet() {
    var min_length = 1; // min caracters to display the autocomplete
    var keyword = $('.ingredient').val();
    if (keyword.length >= min_length) {
        $.ajax({
            url: 'ajax_refresh.php',
            type: 'POST',
            data: {keyword:keyword},
            success:function(data){
                $('#ingredient_list_id').show();
                $('#ingredient_list_id').html(data);
            }
        });
    } else {
        $('#ingredient_list_id').hide();
    }
}

function autocomplet2() {
    var min_length = 1; // min caracters to display the autocomplete
    var keyword = $('.ingredient').val();
    if (keyword.length >= min_length) {
        $.ajax({
            url: 'ajax_refresh.php',
            type: 'POST',
            data: {keyword:keyword},
            success:function(data){
                $('#ingredient_list_id').show();
                $('#ingredient_list_id').html(data);
            }
        });
    } else {
        $('#ingredient_list_id').hide();
    }
}

// set_item : this function will be executed when we select an item
function set_item(item) {
    // change input value
    $('.ingredient').val(item);
    // hide proposition list
    $('#ingredient_list_id').hide();
}


Drugi input nie działa, tzn. nie ma podpowiedzi w ogóle, pierwszy input działa normalnie z tym że wybrana opcja pojawia się w wszystkich inputach.

Ten post edytował lustfingers 31.03.2017, 18:08:27
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 14.08.2025 - 20:19