开发者

jquery autocomplete extra spaces

开发者 https://www.devze.com 2023-02-04 22:24 出处:网络
I got this loop in a jsp file <%for (int i = 0; i < length; i++) { for( int j = 0; j < width; j++)

I got this loop in a jsp file

<%    for (int i = 0; i < length; i++)
                 {
                    for( int j = 0; j < width; j++)
                    {
                        element = MAP_LIST[j][i];
                        if (element.equals("A"))
                        {} else if (j == width-1 && i == length-1){
                        %>
                        <%=element%><%}
                        else
                        {
                        %>
                        <%=element%>,<%}
                    }
                 } 
%>

which gets me a csv list from an oracle database for my autocomplete text field by using jquery

function Mapsheets(type,nomos)
{   
    $(function() {
        var f_data;

        $.get('/gaec_web/MapShe开发者_StackOverflowets.jsp',{'datasrc-select':datasource, 'type_1': type, 'nomos': nomos}, function(data){
            f_data = data.split(',');
        $( "#fx_no" ).autocomplete({
            source: f_data,
            minLength: 2
        });
        });
            });
}

everything works like a charm, i type the first 2 chars and the autocomplete pops up displays every thing as it was supposed to and when I try to pick a value i get the value with several (5) extra spaces in the tail. And then when it gets submitted it fails cause it doesnt match the mapname in question. the results look like this

"     320-197"

So what is causing this? if i run the jsp page alone also get normal results for example

372-146, 376-146, 372-149, 368-149, 376-149, 380-149, 380-152, 376-152, 372-152, 368-152, 368-155, 376-155, 372-155, 380-155, 368-158, 380-158, 376-158, 372-158

thanks in advance


I never used jsp, but I guess that the reason of the extra spaces is the indentation you use inside the for loop

                   {} else if (j == width-1 && i == length-1){
                    %>
#there are extra space<%=element%><%}
                    else
                    {
                    %>
                    <%=element%>,<%}
                }

you should put the code in this way and maybe the spaces will be ignored:

                   {} else if (j == width-1 && i == length-1){
                    %><%=element%><%}
                    else
                    {
                    %><%=element%>,<%}
                }


I've never used JSP either. but would it be a viable option for you to output your array as json encoded? jQuery can easily parse an array passed as json_encoded.

I googled for json_encode equivalent for JSP and it gave me this link. What is the JSP equivalent to json_encode ( in PHP )?

in your javascript you then use $.getJSON(url, [data], [callback]) just like you used to do. except for now, the data parameter of the callback function holds a json-decoded array, ergo your previous normal array, but then in javascript-form.

Just provide that data var to the source for the autocomplete.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号