How to use array in autocomplete textbox jquery?

你离开我真会死。 提交于 2019-12-12 17:30:41

问题


For Example

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<script>
    $(function() { 
        var availableTags = [ "ActionScript", "AppleScript", "Asp"];
        var availableTagsCode = ["1", "2", "3"]; 
        $( "#tags" ).autocomplete({ source: availableTags });
    });
</script>

<input id="tags" name="name">
<input id="tags_code" name="code">

Actually, I have tried to change code while select suggestion using following code:

$("#tags_code").val(availableTagsCode); 

I need to select suggestion test if i will select 0th array from tag the 0th code should assign to name="code" textbox.
Please help me to resolve this problem.


回答1:


You can define a select event handler:

$( "#tags" ).autocomplete({ 
    source: availableTags,
    select: function(event, ui) {
        var index = availableTags.indexOf(ui.item.value);
        $("#tags_code").val(availableTagsCode[index]);  
    }
});

Here is the working JSFiddle demo.

Actually, jQuery UI allows you to use an array of objects with value and label properties when you provide source.
So, something like this will work and look better:

var tags = [
    {"label": "ActionScript", "value": 1},
    {"label": "AppleScript", "value": 2},
    {"label": "Asp", "value": 3}
];

$( "#tags" ).autocomplete({ 
    source: tags,
    select: function (event, ui) {
        $("#tags_code").val(ui.item.value);  
    }
});

JSFiddle demo for the second approach.

If you don't want the value to be replaced after choosing, you can use custom property instead of value:

var tags = [
    {"label": "ActionScript", "code": 1},
    {"label": "AppleScript", "code": 2},
    {"label": "Asp", "code": 3}
];

$( "#tags" ).autocomplete({ 
    source: tags,
    select: function (event, ui) {
        $("#tags_code").val(ui.item.code);  
    }
});

JSFiddle demo.




回答2:


Set select event handler

$(function() {
  var availableTags = ["ActionScript", "AppleScript", "Asp"];
  var availableTagsCode = ["1", "2", "3"];
  $("#tags").autocomplete({
    source: availableTags,
    select: function(e, ui) {
      $('#tags_code').val(availableTagsCode[availableTags.indexOf(ui.item.value)]);
    }
  });
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<input id="tags" name="name">
<input id="tags_code" name="code">



回答3:


<script type="text/javascript">  
    $(document).ready(function () {
        $("#textBoxID").autocomplete({
            source: function (request, responce) {
                $.ajax({
                    url: "webservice_Name.asmx/webservice_functionName",
                    method: "post",
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify({ parameterName: request.term }),
                    dataType: 'json',
                    success: function (data) {

                        responce(data.d);
                        if (data.d.length > 0)
                            $('#spnError').text("");
                        else
                            $('#spnError').text("Not Matching Any Result");
                        console.log(data.d.length);
                    },
                    error: function (err) {
                        alert(err);
                    }
                });
            },
            minLength: 2,
            select: function (event, ui) {
                console.log("You selected: " + ui.item.label);
                $.ajax({
                    url: "webservice_Name.asmx/webservice_functionNameForgetID",
                    method: "post",
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify({ parameterName: ui.item.label }),
                    dataType: 'json',
                    success: function (data) {
                        console.log(data.d);

                    },
                    error: function (err) {
                        alert(err);
                    }
                });
            }
        });
    });
</script>


来源:https://stackoverflow.com/questions/33429866/how-to-use-array-in-autocomplete-textbox-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!