我收到javascript错误在textarea字段中没有数字“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>
答案 0 :(得分:1)
该行:
+ ui.item.value
正在使用一元+
运算符将值强制转换为数字。尝试将非数字字符串强制转换为数字将产生NaN
。
从该行中删除+
,您的代码应该可以正常运行:http://jsfiddle.net/gEkWF/6/