jQuery自动完成.选择后,提醒其值在建议的 [英] jQuery Autocomplete. On select, alert a value of which is in the suggestion's

查看:128
本文介绍了jQuery自动完成.选择后,提醒其值在建议的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的jQuery自动完成插件可用于电影搜索.每部电影的ID均来自IMDb的ID.现在,我解析该ID的方式来自PHP,并用class ="imdbID"将其包含在一个范围内.所以看起来像这样:

<li>Movie title 1<span class="imdbID">tt345813</span></li>
<li>Movie title 1<span class="imdbID">tt346413</span></li>
<li>Movie title 1<span class="imdbID">tt789675</span></li>
<li>Movie title 1<span class="imdbID">tt185858</span></li>

然后继续.这是一个真实示例的外观: http://www.screenr.com/aH0s

现在,我要选择一部电影并提醒该imdbID值.

如果我使用此方法:

select:function(a,b){
        var bbbb = $(".imdbID").text();
        alert(bbbb);
}

我得到所有跨度的值,如下所示:

所以我的问题是,如何仅获取所选自动完成值的.imdbID?

谢谢

解决方案

参考您对PHP代码的其他问题,我会做类似的事情(请带点盐,我不知道PHP ):

$tmdb = new TMDb($api_key);
$json = json_decode($tmdb->searchMovie($_GET['term']));
$response = array();
$i=0;
foreach($json as $movie){
    if(!$movie->imdb_id || $movie->adult) continue;
    if($i >= 6) break;
    $response[$i]['value'] = $movie->name;
    $response[$i]['label'] = $movie->name . ' <small>(' . date('Y',strtotime($movie->released)).')</small><span><span> (' . $movie->imdb_id . ')';
    $response[$i]['imdbid'] = $movie->imdb_id;
    $i++;
}
echo json_encode($response);

想法是您将imdbid添加为要发送到客户端的JSON有效负载的属性.

然后在选择事件上:

$("input").autocomplete({
    /* snip */
    select: function (event, ui) {
        alert(ui.item.imdbid); // <-- alert the imdbid property
    }
});

I jQuery autocomplete plugin for a movie search. Each movie gets an id of which is from IMDb's ID. Now the way I parse that ID is from PHP and is enclosed inside a span with class="imdbID". So it looks something likes this:

<li>Movie title 1<span class="imdbID">tt345813</span></li>
<li>Movie title 1<span class="imdbID">tt346413</span></li>
<li>Movie title 1<span class="imdbID">tt789675</span></li>
<li>Movie title 1<span class="imdbID">tt185858</span></li>

and goes on. Here's how it looks with a real example: http://www.screenr.com/aH0s

Now, I want to select a movie and alert that imdbID value.

If I use this method:

select:function(a,b){
        var bbbb = $(".imdbID").text();
        alert(bbbb);
}

I get all the span's value like this:

So my question is, how can I get only the .imdbID of the selected autocomplete value?

Thank you

解决方案

Referencing your other question for the PHP code, I would do something like this (please take with a grain of salt, I do not know PHP):

$tmdb = new TMDb($api_key);
$json = json_decode($tmdb->searchMovie($_GET['term']));
$response = array();
$i=0;
foreach($json as $movie){
    if(!$movie->imdb_id || $movie->adult) continue;
    if($i >= 6) break;
    $response[$i]['value'] = $movie->name;
    $response[$i]['label'] = $movie->name . ' <small>(' . date('Y',strtotime($movie->released)).')</small><span><span> (' . $movie->imdb_id . ')';
    $response[$i]['imdbid'] = $movie->imdb_id;
    $i++;
}
echo json_encode($response);

The idea being that you add imdbid as a property of the JSON payload you're sending to the client.

Then on the select event:

$("input").autocomplete({
    /* snip */
    select: function (event, ui) {
        alert(ui.item.imdbid); // <-- alert the imdbid property
    }
});

这篇关于jQuery自动完成.选择后,提醒其值在建议的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆