Romain Romain - 3 months ago 99
Javascript Question

EasyAutocomplete: change the returned value

I would like to use the EasyAutcomplete plugin to create an autocomplete list for the user and then to send the value associated to the matching string by GET method ... and not the matching string.

Using this code

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<link rel="stylesheet" href="./EasyAutocomplete/easy-autocomplete.min.css">
<link rel="stylesheet" href="./EasyAutocomplete/easy-autocomplete.themes.min.css">
<script src="./EasyAutocomplete/jquery.easy-autocomplete.min.js"></script>
</head>
<body>
<form method="get" action="test.php">
<input id="provider-file" name="get_value"/>
<script>
$(document).ready(function() {
var options = {
url: "./EasyAutocomplete/file.json",
getValue: "name",
list: {
match: {
enabled: true
}
}
};
$("#provider-file").easyAutocomplete(options);
});
</script>
<div>
<a href=""><input type="submit" value="send"/></a>
</div>
</form>
</body>
</html>


and the JSON file having the following format

[
{"name":"Bob","id":"1"},
{"name":"David","id":"2"},
{"name":"Steve","id":"3"},
...
]


the user get redirected to 'test.php?get_value=Bob' after submitting Bob, while I would like him to be redirected to 'test.php?get_value=1' (i.e. to send "id" and not "name").

Could anyone help me? I couldn't find any answer in the EasyAutocomplete doc.

Answer

You can use the onSelectItemEvent event along with the getSelectedItemData method to populate a hidden input field named get_value with the ID you want to submit and remove the name from the lookup input field so that it won't get submitted.

HTML:

<input id="provider-file">
<input type="hidden" id="id-value" name="get_value">

Setup:

  var options = {
    url: "./EasyAutocomplete/file.json",
    getValue: "name",
    list: {
      onSelectItemEvent: function() {
        var value = $("#provider-file").getSelectedItemData().id; //get the id associated with the selected value
        $("#id-value").val(value).trigger("change"); //copy it to the hidden field
      }
    }
  };

Demo (click submit to see that get_value=id is submitted):

$(document).ready(function() {
  var options = {
    data: [{
      "name": "Bob",
      "id": "1"
    }, {
      "name": "David",
      "id": "2"
    }, {
      "name": "Steve",
      "id": "3"
    }],
    getValue: "name",
    list: {
      onSelectItemEvent: function() {
            var value = $("#provider-file").getSelectedItemData().id; //get the id associated with the selected value
            $("#id-value").val(value).trigger("change"); //copy it to the hidden field
      }
    }
  };
  $("#provider-file").easyAutocomplete(options);

  // used to validate what gets submitted
  $("#form-test").on('submit', function(e) {
    e.preventDefault();
    console.log($(this).serialize());
  });
});
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/easy-autocomplete/1.3.5/easy-autocomplete.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/easy-autocomplete/1.3.5/easy-autocomplete.themes.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/easy-autocomplete/1.3.5/jquery.easy-autocomplete.min.js"></script>

<form method="get" id="form-test">
  <input id="provider-file">
  <input type="hidden" id="id-value" name="get_value">
  <input type="submit">
</form>