$(function() {
var cache = {}
function split( val ) {
return val.split( /,\s*/ )
}
function extractLast( term ) {
return split( term ).pop()
}
$( "#tags" )
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).data( "ui-autocomplete" ).menu.active ) {
event.preventDefault()
}
})
.autocomplete({
minLength: 1,
source: function (request, response) {
var term = request.term
if ( term in cache ) {
response( cache[ term ] )
return
}
$.ajax({
type: "POST",
url: mlog.variable.base+"/admin/post/autocomplete?keyword=" + extractLast( request.term ) ,
contentType: "application/jsoncharset=utf-8",
dataType: "json",
success: function (data) {
cache[ term ] = $.map(data, function(item){
return item.name
})
response( $.ui.autocomplete.filter(
$.map(data, function(item){
return item.name
}), extractLast( request.term ) ) )
},
error: function () {
alert("补全失败")
}
})
},
focus: function() {
// prevent value inserted on focus
return false
},
select: function( event, ui ) {
var terms = split( this.value )
terms.pop()
terms.push( ui.item.value )
terms.push( "" )
this.value = terms.join( ", " )
return false
}
})
})
var v=document.getElementById("文本框ID")if(v.length>0){
var n=parseFloat(v.value)
v.value=n.toFixed(2).toString()
}
补全后的代码如下:
function getSecret(file,secretPassword) {file.opened = file.opened +1
if (secretPassword == file.password) {
return file.contents
} else {
return "Invalid password! No secret for you."
}
}
function setSecret(file,secretPassword, secret) {
if (secretPassword == file.password) {
file.opened = 0
file.contents = secret
}
}
var superSecretFile = {
level: "classified",
opened: 0,
password: 2,
contents: "Dr.Evel's next meeting is in Detroit."
}
var secret = getSecret(superSecretFile, 2)
console.log(secret)
setSecret(superSecretFile, 2, "Dr.Evels next meeting is in philadelphia.")
secret = getSecret(superSecretFile, 2)
console.log(secret)