爲什麼我在alert
請致電undef
?Javascript:可變範圍問題
#!/usr/bin/env perl
use warnings;
use 5.014;
use utf8;
use Mojolicious::Lite;
use DBI;
my $db = 'my_test_db.db';
my $table = 'my_test_table';
my $dbh = DBI->connect("dbi:SQLite:dbname=$db", '', '',
{ RaiseError => 1, PrintError => 0, AutoCommit => 1, sqlite_unicode => 1, }
) or die $DBI::errstr;
$dbh->do("CREATE TEMP TABLE $table (str TEXT, num INTEGER)");
my $sth = $dbh->prepare("INSERT INTO $table (str, num) VALUES (?, ?)");
$sth->execute('aaa', '111');
$sth->execute('bbb', '222');
$sth->execute('ccc', '333');
get '/eingabe' => sub {
my $self = shift;
$self->render('eingabe');
};
get '/search_db/:col' => sub {
my $self = shift;
my $col = $self->param('col');
my $term = $self->param('term');
my $sth = $dbh->prepare("SELECT DISTINCT $col FROM $table WHERE $col LIKE ?");
$sth->execute($term . '%');
my $ref;
while (my $row = $sth->fetchrow_arrayref()) {
push @$ref, @$row;
}
$self->render(json => $ref);
};
app->start;
__DATA__
@@ eingabe.html.ep
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var ids = [ 'str', 'num' ];
for (var i = 0; i < ids.length; i++){
$("#" + ids[i]).autocomplete({
source: function(request, response){
alert(ids[i]); // <---
$.getJSON('/search_db/' + ids[i], request, function(data_from_server){
var suggestions = [];
var len = data_from_server.length;
for(var i = 0; i < len; i++){
suggestions.push(data_from_server[i]);
}
response(suggestions);
});
}
});
}
});
</script>
</head>
<body>
<form>
<table>
<tr><td>String:</td><td><input type="text" id="str" name="str"" /></td></tr>
<tr><td>Number:</td><td><input type="number" id="num" name="num" /></td></tr>
</table><br />
<input type="submit" value="OK"/>
</form>
</body>
</html>
是的,這就是我想說的,但我很着急。很好的解釋。 –