PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Sep 17, 2019 12:25 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 35 posts ]  Go to page Previous  1, 2, 3
Author Message
PostPosted: Mon Sep 18, 2017 4:07 am 
Offline
Forum Contributor

Joined: Wed Sep 25, 2013 4:09 am
Posts: 172
Hmm I can't get the function to set the current list when pressing enter... also for some reason everytime i press enter it automatically add another input field (ingredient: (inputfield) remove) just like when i click the add new ingredient button.

I don't know why it does that when i have one of the inputfields in focus and added e.preventDefault() on keypress enter... (I would maybe understand why it did so if the add new ingredient button was in focus when pressing enter but it is not)..


I still use the ean autocomplete function. I have 5 eans starting with 7 and 1 starting with 8.

Now the program behaves like this:

* if i type something not in db (I dont get a list suggestion) and hit enter it just clear the input field and add a new row (which it shouldnt add).

* if i type 7 it display list of eans starting with 7 and if i hit enter it set the inputbox to the first ean (say 7654) in the list which it should. but adds another input field which it shouldnt.

* if i click on the next inputfield and type in 8 it display 8989 in the list which it should but when i press enter it sets the first ean 7654 again instead of 8989. and add another row which it shouldnt..

So how do i do to update the list because it show the correct list when i type in the number but always sets from the first list created..

if i reload page and press 8 in the first field and hit enter it set 8989 as it should.
if i in the next field press 7 it shows all ean starting with 7 but when i hit enter it sets 8989... so it always set the first list it loads?


Syntax: [ Download ] [ Hide ]
$(function() {
   
   
    /* Function to add or remove ingredient or allergen input fields*/
   
        var i = 1;
        var j = 1;
        var k = 2; // gives a unique ID to each added field (does not decrement as i and j)
        $('#add_ingredient').click(function() {
               $('#Ingredient_table').append('<tr><td><p>Ingredient</p></td><td><div class="input_container"><input type="text"  size="20" name="ingredient[]" value="" placeholder="Input Value" /><ul id="ingredient_list_id_'+k+'" tabindex="-1"></ul></div></td><td><a href="#" class="remove_field"><p style="font-size:50%;">Remove</p></a></td></tr>');
                i++; k++;
                return false;
        });
       
        $('#Ingredient_table').on("click",".remove_field",function(e){
            e.preventDefault();    
            $(this).parents('tr').remove();  //parents could be switched with closest
            i--;
        })
       
        $('#add_allergen').click(function() {
               $('#Allergen_table').append('<tr><td><p>Allergen</p></td><td><input type="text" id="allergenid_'+k+'" size="20" name="allergen['+k+']" value="'+k+'" placeholder="Input Value" /></td><td><a href="#" class="remove_field"><p style="font-size:50%;">Remove</p></a></td></tr>');
                j++; k++;
                return false;
        });
       
        $('#Allergen_table').on("click",".remove_field",function(e){
            e.preventDefault();    
            $(this).parents('tr').remove();  //parents could be switched with closest
            j--;
        })
       
        $('#ingredient_wrapper').on("keyup", ".input_container", function(e){
            if(e.which == 13) {
             e.preventDefault();          
             //$(this).children().val($('li').first().text());
             $(this).children().val($('li').first().text());
             $(this).children("ul").hide();
             return;
            }
           
            var thislist = $(this).children("ul");
            var min_length = 0;    
            var keyword = $(this).children().val();
            if (keyword.length >= min_length) {
                        $.ajax({
                                url: 'ajax_php/ajax_browserefresh.php',
                                type: 'POST',
                                data: {keyword:keyword},
                                success:function(data){
                                    var result = JSON.parse(data);
                                    var $nodes = $([]);
                                   
                        $.each(result, function(key, value) {
                            var $li = $("<li></li>").text(value.ean);
                            $li.click(function() {
                                set_item_in(thislist, value.ean);
                            });
                            $nodes=$nodes.add($li);
                        });
                       
                        if (result[0] == -1) {
                            thislist.hide();
                        } else {
                            thislist.empty().append($nodes).show();    
                        }
                                }                                       /* Close on sucess */
                        });                                         /* Close ajax      */
            } else {
                    thislist.hide();
            }
           
           
        }) // end of on keyup
       
       
       
});

function set_item_in(elem, item) {
   // $("#"+elem).val(item);   /*For inputboxes textareas and so on else text*/
    //elem.hide();
    //elem.text(item);
    elem.parent().children().val(item);
    elem.hide();
}
 


EDIT
I fixed the add row thing by prevent default on keydown instead.. but I still can't get it to set the correct list instead of the first list it loads....


Top
 Profile  
 
PostPosted: Mon Sep 18, 2017 5:09 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA


Top
 Profile  
 
PostPosted: Mon Sep 18, 2017 5:27 am 
Offline
Forum Contributor

Joined: Wed Sep 25, 2013 4:09 am
Posts: 172


Top
 Profile  
 
PostPosted: Mon Sep 18, 2017 6:19 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
In that callback, this will be the #ingredient_wrapper. If you want to start from the .input_container that [contains the element that] triggered the event then
Syntax: [ Download ] [ Hide ]
var $container = $(e.target /* the input */).closest(".input_container");

Then you can navigate from there.


Top
 Profile  
 
PostPosted: Mon Sep 18, 2017 6:25 am 
Offline
Forum Contributor

Joined: Wed Sep 25, 2013 4:09 am
Posts: 172


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 35 posts ]  Go to page Previous  1, 2, 3

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group