“select”标签的占位符?
我想知道如何使用 select
标签实现 placeholder
效果,如果有默认选择的选项(例如“请选择一个选项:”),那就太好了。
我尝试了一些变体,但到目前为止它们还没有工作,我确信它可以实现,因为我在某个地方看到过它(不记得在哪里)。
我已经尝试过:
1)
<fieldset>
<select data-placeholder="Please select a product" id="s1" class="global">
<option value="Some">Some</option>
<option value="Slower">Slower</option>
<option value="Slow">Slow</option>
<option value="Fast">Fast</option>
<option value="Faster">Faster</option>
</select>
</fieldset>
2)
<fieldset>
<select id="s1" class="global">
<option data-placeholder="true" value="Some">Some</option>
<option value="Slower">Slower</option>
<option value="Slow">Slow</option>
<option value="Fast">Fast</option>
<option value="Faster">Faster</option>
</select>
</fieldset>
两者都不起作用,也许有一个 jQuery 方法可以做到这一点?
快速编辑:我不想只禁用其中一个选项并将其选中
,因为它仍会与其他项目一起显示在下拉列表中。
I'm wondering how to achieve the placeholder
effect with the select
tag, it would be really nice to have the default selected option something like "Please Chose an Option:".
I have tried some variations and they are not working so far and I'm sure that it can be achieved cause i seen it somewhere (can't remember where).
I have tried this:
1)
<fieldset>
<select data-placeholder="Please select a product" id="s1" class="global">
<option value="Some">Some</option>
<option value="Slower">Slower</option>
<option value="Slow">Slow</option>
<option value="Fast">Fast</option>
<option value="Faster">Faster</option>
</select>
</fieldset>
2)
<fieldset>
<select id="s1" class="global">
<option data-placeholder="true" value="Some">Some</option>
<option value="Slower">Slower</option>
<option value="Slow">Slow</option>
<option value="Fast">Fast</option>
<option value="Faster">Faster</option>
</select>
</fieldset>
Both are not working, maybe there is a jQuery method to make that?
Quick edit: I DON'T want to just disable one of the options and make it selected
because it will still be showed in the drop-down list with the other items.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这里有两种类型的占位符,可重新选择和隐藏:
演示: http://jsfiddle.net/lachlan/FuNmc/
可重新选择占位符:
隐藏占位符:
用于更改第一项颜色的 CSS:
以及一些用于在选择后切换字体颜色的 jQuery:
灵感:
https://stackoverflow.com/a/5805194/1196148 - 可重新选择的占位符
https://stackoverflow.com/a/8442831/1196148 - 隐藏占位符
Here's two types of placeholder, re-selectable and hidden:
Demo: http://jsfiddle.net/lachlan/FuNmc/
Re-selectable placeholder:
Hidden placeholder:
CSS to change the colour of the first item:
And a little jQuery to toggle the font-color after selection:
Inspiration:
https://stackoverflow.com/a/5805194/1196148 - re-selectable placeholder
https://stackoverflow.com/a/8442831/1196148 - hidden placeholder
我按照@Truth建议构建了一个简单的演示: http://jsfiddle.net/6QnXF/
我希望它对你有用。
I've built a simple demo by following @Truth suggestion: http://jsfiddle.net/6QnXF/
I hope it works for you.
据我所知,单独使用 HTML 是不可能做到这一点的(尽管那会很好。)
您可以使用选定的选项来伪造它(我知道您不想要它,但这可能是唯一的方法)。选择另一个选项后,您可以将其删除。
这是我所描述内容的一个工作示例。
As far as I know, there's no way doing it with HTML alone (though that would be nice.)
You can fake it with a selected option (I know you don't want it, but it's probably the only way). Once another option is selected, you can remove it.
Here's a working example of what I describe.
我的猜测是,您将必须为选择框构建自己的解决方法。类似一系列充当选项的 div,单击时会显示这些选项,并将其值插入到隐藏的输入字段中。
My guess is that you're going to have to construct your own workaround for a select box. Something like a series of divs that act as options which when clicked are shown and have their value inserted into a hidden input field.
试试这个:
中添加:
在 css(样式表):和 HTML
try this:
add this in css(style sheet):
and the HTML: