最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - EasyAutocomplete: change the returned value - Stack Overflow

matteradmin8PV0评论

I would like to use the EasyAutplete plugin to create an autoplete 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/jquery-1.11.2.min.js"></script>
    <link rel="stylesheet" href="./EasyAutoplete/easy-autoplete.min.css"> 
    <link rel="stylesheet" href="./EasyAutoplete/easy-autoplete.themes.min.css">
    <script src="./EasyAutoplete/jquery.easy-autoplete.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: "./EasyAutoplete/file.json",
                getValue: "name",
                list: {
                    match: {
                        enabled: true
                        }
                    }
                };
            $("#provider-file").easyAutoplete(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 EasyAutoplete doc.

I would like to use the EasyAutplete plugin to create an autoplete 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./jquery-1.11.2.min.js"></script>
    <link rel="stylesheet" href="./EasyAutoplete/easy-autoplete.min.css"> 
    <link rel="stylesheet" href="./EasyAutoplete/easy-autoplete.themes.min.css">
    <script src="./EasyAutoplete/jquery.easy-autoplete.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: "./EasyAutoplete/file.json",
                getValue: "name",
                list: {
                    match: {
                        enabled: true
                        }
                    }
                };
            $("#provider-file").easyAutoplete(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 EasyAutoplete doc.

Share Improve this question asked Aug 29, 2016 at 16:13 RomainRomain 1412 silver badges9 bronze badges 2
  • perhaps changing the getValue: "name" to "id"? – celerno Commented Aug 29, 2016 at 16:15
  • @celerno: actually not ... getValue also refers to the field to look into ... so changing getValue to "id" will induce no match for "Bob" or "David", ... But thanks. – Romain Commented Aug 29, 2016 at 16:17
Add a ment  | 

1 Answer 1

Reset to default 5

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: "./EasyAutoplete/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").easyAutoplete(options);

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

Post a comment

comment list (0)

  1. No comments so far