jQuery UI autocomplete multiple values with spaces

2019-08-30 01:36发布

问题:

I'm using jQuery UI autocomplete multiple values example here: http://jqueryui.com/autocomplete/#multiple

It works fine except if you happen to have whitespace in your values. Then the separator gets plonked into the middle of the value when you add the next value.

Any idea how I might prevent this.

I basically have lots of values in my source array that are sentences, and I want to be able to add them via autocomplete and have them separated by " & ".

回答1:

I created an example fiddle here. It is based on the example on the JQuery UI pages.

The source contains some sentences:

['This is some sentence', 'While this is another one', 'Just filling up some space']

splits on &...

this.value = terms.join( " & " );

.... and (this might have been your problem) has an updated version of the split function in the example:

function split( val ) {
    return val.split(" & ");
}