我已经实现了这个选择列表,由 Volker Otto 在 Codepen 上提供: http://codepen.io/l4ci/pen/gPjYma/
我发现它非常适合我,但是我不知道如何处理这个选择列表的用户选择,因为它不是一个选项列表。
有什么想法吗?
在下面找到标记、css 和 js:
.select {
position: relative;
display: block;
margin: 0 auto;
width: 100%;
max-width: 325px;
color: #cccccc;
vertical-align: middle;
text-align: left;
user-select: none;
-webkit-touch-callout: none;
}
.select .placeholder {
position: relative;
display: block;
background-color: #393d41;
z-index: 1;
padding: 1em;
border-radius: 2px;
cursor: pointer;
}
.select .placeholder:hover {
background: #34383c;
}
.select .placeholder:after {
position: absolute;
right: 1em;
top: 50%;
transform: translateY(-50%);
font-family: 'FontAwesome';
content: '\f078';
z-index: 10;
}
.select.is-open .placeholder:after {
content: '\f077';
}
.select.is-open ul {
display: block;
}
.select ul {
display: none;
position: absolute;
overflow: hidden;
width: 100%;
background: #fff;
border-radius: 2px;
top: 100%;
left: 0;
list-style: none;
margin: 5px 0 0 0;
padding: 0;
z-index: 100;
}
.select ul li {
display: block;
text-align: left;
padding: 0.8em 1em 0.8em 1em;
color: #999;
cursor: pointer;
}
.select ul li:hover {
background: #4ebbf0;
color: #fff;
}
$('.select').on('click','.placeholder',function(){
var parent = $(this).closest('.select');
if ( ! parent.hasClass('is-open')){
parent.addClass('is-open');
$('.select.is-open').not(parent).removeClass('is-open');
}else{
parent.removeClass('is-open');
}
}).on('click','ul>li',function(){
var parent = $(this).closest('.select');
parent.removeClass('is-open').find('.placeholder').text( $(this).text() );
});
$('.select').on('click', '.placeholder', function() {
var parent = $(this).closest('.select');
if (!parent.hasClass('is-open')) {
parent.addClass('is-open');
$('.select.is-open').not(parent).removeClass('is-open');
} else {
parent.removeClass('is-open');
}
}).on('click', 'ul>li', function() {
var parent = $(this).closest('.select');
parent.removeClass('is-open').find('.placeholder').text($(this).text());
});
@import 'https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css';
*,
*:after,
*:before {
box-sizing: border-box;
}
html {
box-sizing: inherit;
background: linear-gradient(to left, #8e9eab, #eef2f3);
}
body {
margin: 10% auto;
text-align: center;
font-size: 12px;
}
.select {
position: relative;
display: block;
margin: 0 auto;
width: 100%;
max-width: 325px;
color: #cccccc;
vertical-align: middle;
text-align: left;
user-select: none;
-webkit-touch-callout: none;
}
.select .placeholder {
position: relative;
display: block;
background-color: #393d41;
z-index: 1;
padding: 1em;
border-radius: 2px;
cursor: pointer;
}
.select .placeholder:hover {
background: #34383c;
}
.select .placeholder:after {
position: absolute;
right: 1em;
top: 50%;
transform: translateY(-50%);
font-family: 'FontAwesome';
content: '\f078';
z-index: 10;
}
.select.is-open .placeholder:after {
content: '\f077';
}
.select.is-open ul {
display: block;
}
.select.select--white .placeholder {
background: #fff;
color: #999;
}
.select.select--white .placeholder:hover {
background: #fafafa;
}
.select ul {
display: none;
position: absolute;
overflow: hidden;
width: 100%;
background: #fff;
border-radius: 2px;
top: 100%;
left: 0;
list-style: none;
margin: 5px 0 0 0;
padding: 0;
z-index: 100;
}
.select ul li {
display: block;
text-align: left;
padding: 0.8em 1em 0.8em 1em;
color: #999;
cursor: pointer;
}
.select ul li:hover {
background: #4ebbf0;
color: #fff;
}
<div class="select">
<span class="placeholder">Select your language</span>
<ul>
<li>España- Español</li>
<li>United States - English</li>
<li>France - Français</li>
<li>Deutschland - Deutsch</li>
</ul>
</div>
<br>
<div class="select select--white">
<span class="placeholder">Select your language</span>
<ul>
<li>España- Español</li>
<li>United States - English</li>
<li>France - Français</li>
<li>Deutschland - Deutsch</li>
</ul>
</div>
最佳答案
$('.select').on('click', '.placeholder', function() {
var parent = $(this).closest('.select');
if (!parent.hasClass('is-open')) {
parent.addClass('is-open');
$('.select.is-open').not(parent).removeClass('is-open');
} else {
parent.removeClass('is-open');
}
}).on('click', 'ul>li', function() {
alert($(this).text())//this is the value of the text you selected
var parent = $(this).closest('.select');
parent.removeClass('is-open').find('.placeholder').text($(this).text());
});
@import 'https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css';
*,
*:after,
*:before {
box-sizing: border-box;
}
html {
box-sizing: inherit;
background: linear-gradient(to left, #8e9eab, #eef2f3);
}
body {
margin: 10% auto;
text-align: center;
font-size: 12px;
}
.select {
position: relative;
display: block;
margin: 0 auto;
width: 100%;
max-width: 325px;
color: #cccccc;
vertical-align: middle;
text-align: left;
user-select: none;
-webkit-touch-callout: none;
}
.select .placeholder {
position: relative;
display: block;
background-color: #393d41;
z-index: 1;
padding: 1em;
border-radius: 2px;
cursor: pointer;
}
.select .placeholder:hover {
background: #34383c;
}
.select .placeholder:after {
position: absolute;
right: 1em;
top: 50%;
transform: translateY(-50%);
font-family: 'FontAwesome';
content: '\f078';
z-index: 10;
}
.select.is-open .placeholder:after {
content: '\f077';
}
.select.is-open ul {
display: block;
}
.select.select--white .placeholder {
background: #fff;
color: #999;
}
.select.select--white .placeholder:hover {
background: #fafafa;
}
.select ul {
display: none;
position: absolute;
overflow: hidden;
width: 100%;
background: #fff;
border-radius: 2px;
top: 100%;
left: 0;
list-style: none;
margin: 5px 0 0 0;
padding: 0;
z-index: 100;
}
.select ul li {
display: block;
text-align: left;
padding: 0.8em 1em 0.8em 1em;
color: #999;
cursor: pointer;
}
.select ul li:hover {
background: #4ebbf0;
color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <!-- You did not include this on your OP -->
<div class="select">
<span class="placeholder">Select your language</span>
<ul>
<li>España- Español</li>
<li>United States - English</li>
<li>France - Français</li>
<li>Deutschland - Deutsch</li>
</ul>
</div>
<br>
<div class="select select--white">
<span class="placeholder">Select your language</span>
<ul>
<li>España- Español</li>
<li>United States - English</li>
<li>France - Français</li>
<li>Deutschland - Deutsch</li>
</ul>
</div>
我在您设置值的地方添加了警报,请检查代码中的注释
关于jquery - 在我的表单中实现一个选择元素,但不确定如何获取列表的用户选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36370862/