Clear form field, if autocomplete is null

2

I am using the code for autocomplete below. I type the initials of a fruit and my select returns with the names of the fruits that match those initials. Selecting one of them, I get the id and play in another field. So far the problem is that I wanted it if nothing was found, clear the field with id.

Example:

I open the form and fill in "ma" will appear papaya and if I select papaya the cod 1 goes to the field idFruta off and type "li" appears lemon and idFruta is filled with 2 that everything is working .

Now if you type "zz" nothing appears, just the message "fruit not found" but my idFruta field remains with id of the fruit selected previously. I then wanted to select "No Fruit" to clear my field idFruta .

Can you do that?

jQuery.fn.autoCompleteEditora = function (idCod, idFocus, formAddFruta) {
    $(this).autocomplete({
        appendTo: formAddFruta,
        source: function (request, response) {
            $("#loadingFruta").show();
            $.ajax({
                url: '../fruta/frutas.json',
                type: "GET",
                dataType: "json",
                contentType: "application/x-www-form-urlencoded;charset=UTF-8",
                data: {
                    q: request.term
                },
                success: function (data) {
                    if (!data.length) {
                        var result = [
                            {
                                label: 'Fruta inexistente' ,
                                value: response.term
                            }
                        ];
                        response(result);
                    } else {
                        response($.map(data, function (item) {
                            return {
                                label: item.nome,
                                id: item.cod,
                                abbrev: item
                            };
                        }));
                        $("#loadingFruta").hide();
                    }
                }
            });
        },
        minLength: 2,
        focus: function (event, ui) {
            $(this).val(ui.item.label);
            return false;
        },
        select: function (event, ui) {
            cod = ui.item.abbrev.cod;
            idCod.val(cod);
            (idFocus !== null) ? idFocus.focus() : idCod.focus();

        }
    });
    $["ui"]["autocomplete"].prototype["_renderItem"] = function (ul, item) {
        return $("<li></li>")
                .data("item.autocomplete", item)
                .append($("<a></a>").html(item.label))
                .appendTo(ul);
    };
};
    
asked by anonymous 27.12.2016 / 22:41

1 answer

2

You can reset the field inside the if that checks if the ajax request response has no content. Included the following line:

 idCod.val('');    

I'm assuming that idCod is global, since it was used in another event, and I also assume that the posted code is working. It should look like this:

  ...
  if (!data.length) {
      var result = [
         {
          label: 'Fruta inexistente' ,
          value: response.term
         }
      ];
      idCod.val('');    //Incluir esta linha
      response(result);
   } else {
   ...

Update: idCod is within scope. I had not noticed it initially. So that's the way it is.

    
28.12.2016 / 00:48