これは、ドロップダウン コンポーネントがインスタンスが 1 つしかないと想定し、クラス名でバインドするためです。
スクリプトを少し変更するだけで、複数のインスタンスを機能させることができます。変更の概要は以下のとおりです。
デモ: http://jsfiddle.net/fZbx6/1/
変更を強調するためにコメントを入れました。
$(document).ready(function() {
$(".dropdown img.flag").addClass("flagvisibility");
$(".dropdown dt a").click(function() {
// old
//$(".dropdown dd ul").toggle();
// new
$(this).parents(".dropdown").find("dd ul").toggle();
});
$(".dropdown dd ul li a").click(function() {
var text = $(this).html();
// old
//$(".dropdown dt a span").html(text);
//$(".dropdown dd ul").hide();
// NEW
var dd = $(this).parents(".dropdown");
dd.find("dt a span").html(text);
dd.find("dd ul").hide();
$("#result").html("Selected value is: " + getSelectedValue("sample"));
});
function getSelectedValue(id) {
return $("#" + id).find("dt a span.value").html();
}
$(document).bind('click', function(e) {
var $clicked = $(e.target);
if (! $clicked.parents().hasClass("dropdown"))
$(".dropdown dd ul").hide();
});
$("#flagSwitcher").click(function() {
$(".dropdown img.flag").toggleClass("flagvisibility");
});
});