1

我在 textarea 字段中收到一个 javascript 错误 Not a number "NaN"。

我想从自动完成 jqueryui 中选择一个字符串并将所选字段显示到 textarea 中。Json 数组中的数值显示正常,但字符串值显示 NaN。

这是我的 json 数组:

 var financialClasses=[
                "abc",
                "PQR",
                "xyz" ];


// these are the functions for selecting multiple values in the autocomplete textbox

function split( val ) { return val.split( /,\s*/ );    }

function extractLast( term ) { return split( term ).pop();    }  


// This function logs the selected field in the textarea


 function financialclasses_log(message)  
{
            $( "#financialclasses-log" )
.append( message+", ").prependTo( "#financialclasses-log" ); }


    $( "#financialclasses" )/* this function is required when selecting multiple values */
                .bind( "keydown", function( event ) {
                    if ( event.keyCode === $.ui.keyCode.TAB &&
                        $( this ).data( "ui-autocomplete" ).menu.active ) { event.preventDefault(); } })

              .autocomplete({
                    minLength: 0,
                    source: function( request, response ) {
                        // delegate back to autocomplete, but extract the last term
                        response( $.ui.autocomplete.filter(
                            financialClasses, extractLast( request.term ) ) );
                    },
                    focus: function() {
                        // prevent value inserted on focus
                        return false;
                    },
                    select: function( event, ui ) {

                        financialclasses_log( ui.item ?
                            + ui.item.value:
                            "Nothing selected, input was " + this.value );

                        var terms = split( this.value );
                        // remove the current input
                        terms.pop();
                        // add the selected item
                        terms.push( ui.item.value);
                        // add placeholder to get the comma-and-space at the end
                        terms.push( "" );
                        this.value = terms.join( ", " );
                        $(this).val("");
                        return false;
                    }
                });

// here is the html




<div id="financialclass" class="autocomplete_divs" class="ui-widget">
                <fieldset style="padding:0px">
                    <a href="addlist.jsp" style="float:right; margin-right:5px">Add List</a><br>
                    <label for="financialclasses"></label>
                    <input id="financialclasses" size="25">
                    <br>
                    <textarea id="financialclasses-log" class="log" class="ui-widget-content"></textarea>
                    <legend title="Financial Classes"><b>Financial Classes</b></legend>
                </fieldset>
            </div>

http://jsfiddle.net/pratik24/gEkWF/2/

4

1 回答 1

1

该行:

+ ui.item.value

正在使用+将值强制转换为数字的一元运算符。将非数字字符串强制转换为数字的尝试将产生NaN.

从该行中删除+,您的代码应该可以正常工作:http: //jsfiddle.net/gEkWF/6/

于 2013-05-28T20:51:01.133 回答