我用列表来做
JS:
(function ($) {
$.fn.postcodeAddressList = function() {
this.each(function() {
var el = $(this),
li = el.find('li'),
i = li.length;
strippedList(el);
while(i--) {
$(li[i]).on("click", function(e){
strippedList(el);
$(e.currentTarget).attr("selected", "selected");
el.attr("data",$(e.currentTarget).attr("data"));
});
}
});
return this;
};
})(jQuery);
strippedList = function ( el) {
var li = el.find('li'),
i = li.length;
while(i--) {
if($( li[i] ).attr("selected")!= 'defined') {
$(li[i]).removeAttr("selected");
}
if (i%2) {
$(li[i]).attr("stripped","stripped");
}else {
$(li[i]).removeAttr("stripped");
}
}
}
CSS:
.postCode li[stripped="stripped"] {
background-color: #f5f5f5;
}
.postCode li[selected="selected"] {
background-color: #ccc;
}
.postCode li
{
background-image: url('../img/house.png');
background-repeat: no-repeat;
background-position: 5% 50%;
padding: 7px 0 7px 4em;
cursor: pointer;
font-weight: bold;
color: #333;
}
.postCode[disabled="disabled"] {
background-color: #f5f5f5;
border-color: #ddd;
cursor: not-allowed;
}
.postCode {
background-color: white;
list-style: none outside none;
margin-left: -10px;
padding: 5px 0;
width: 340px;
height: 300px;
overflow-y: auto;
border: solid 1px #CCC;
border-radius: 5px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
}
HTML:
<ul class="postCode" data="29392449_0S_F">
<li data="26850525_898354S_F">Andrew Begg 24-26 Upperkirkgate Aberdeen</li>
<li data="31763873_0S_F" stripped="stripped">Flat 1 12 Upperkirkgate Aberdeen</li>
<li data="31763875_0S_F">Flat 1 14 Upperkirkgate Aberdeen</li>
...
</ul>
利用:
...
$('.postCode').postcodeAddressList();
...
...
var value = $('.postCode').attr("data");
...
结果:
Mozilla
铬合金
IE8