How to get desired value from autocomplete jquery function?

My Jquery function:

<script type="text/javascript">

function Origin(sender, args) {

    $(function () {
        $("#<%=txtInfo.ClientID %>").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: '<%=ResolveUrl("~/Webservice.asmx/Get") %>',
                    data: "{ 'prefix': '" + request.term + "'}",
                    dataType: "json",
                    type: "POST",
                    async: false,
                    mustMatch: true,
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        response($.map(data.d, function (item) {
                            return {
                                label: item.split('-')[0],
                                val: item.split('-')[1]
                            }
                        }))
                    },
                    error: function (response) {

                    },
                    failure: function (response) {

                    }
                });
            },

            select: function (e, i) {

                $("#<%=hdnInfo.ClientID %>").val(i.item.val);

                info1()                     

            },                    
            minLength: 0
        }).bind('focus', function () { $(this).autocomplete("search"); });              

        });
   }       

</script>

The return {label: item.split('-')[0],val: item.split('-')[1] } shows that value after split from '-' for example:If the value is "abc-2223" then it returns "abc" But if the value is "A-1 abc-2223" then it returns only "A" but i want "A-1 abc".

Answers


Apply this logic. JsFiddle

<script>
    $(function () {
        var items = ["abc-2223", "A-1 abc-2223"];
        $.each(items, function (key, value) {
            var item = value;

            var text = "";
            var chkItem = item.split('-')[0];
            if (chkItem.length == 1) {
                text = item.substring(item.lastIndexOf("-") + 0, item.length);
                text = item.replace(text, "");
            }
            else {
                text = chkItem;
            }
            alert(text);
        });
    });
</script>

Update

As you requested to change your code; please change success function like below code.

success: function (data) {
        response($.map(data.d, function (item) {

            //Filter item on condition
            var txtLabel = "";
            var chkItem = item.split('-')[0];
            if (chkItem.length == 1) {
                txtLabel = item.substring(item.lastIndexOf("-") + 0, item.length);
                txtLabel = item.replace(txtLabel, "");
            }
            else {
                txtLabel = chkItem;
            }

            return {
                label: txtLabel,
                val: item.split('-')[1] //you have not specified for value field.
            }
        }))
    },

Your server sends in its response an array of strings (data.d).

Have your server directly return an array of {"label":"...","val":"..."} objects instead.


Need Your Help

Multiple file servers for one website's content

php apache fileserver

I'm planning to use multiple file servers to host my website uploaded files. what's the best way to do it ? should I install a web server on other machines as well? or is there any special software...

Python 2.7 os.path.isfile

python python-2.7

I am testing the os.path.isfile(path) method to see if something is a file or not. Strangely I find that if I join the path and file name it returns true, but if I put the full file path it returns...