“select”标签的占位符?

发布于 2024-12-19 11:50:33 字数 1318 浏览 0 评论 0原文

我想知道如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半城柳色半声笛 2024-12-26 11:50:33

这里有两种类型的占位符,可重新选择和隐藏:

演示: http://jsfiddle.net/lachlan/FuNmc/

可重新选择占位符:

<select>
    <option value="" selected>Please select</option>
    <option value="1">Item 1</option>
    <option value="2">Item 2</option>
    <option value="3">Item 3</option>
</select>

隐藏占位符:

<select class="empty">
    <option value="" selected disabled>Please select</option>
    <option value="1">Item 1</option>
    <option value="2">Item 2</option>
    <option value="3">Item 3</option>
</select>

用于更改第一项颜色的 CSS:

select option { color: black; }
select option:first-child { color: grey; }
select.empty { color: grey; }
/* Hidden placeholder */
select option[disabled]:first-child { display: none; }

以及一些用于在选择后切换字体颜色的 jQuery:

// Applies to all select boxes that have no value for their first option
$("select:has(option[value=]:first-child)").on('change', function() {
    $(this).toggleClass("empty", $.inArray($(this).val(), ['', null]) >= 0);
}).trigger('change');

灵感:
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:

<select>
    <option value="" selected>Please select</option>
    <option value="1">Item 1</option>
    <option value="2">Item 2</option>
    <option value="3">Item 3</option>
</select>

Hidden placeholder:

<select class="empty">
    <option value="" selected disabled>Please select</option>
    <option value="1">Item 1</option>
    <option value="2">Item 2</option>
    <option value="3">Item 3</option>
</select>

CSS to change the colour of the first item:

select option { color: black; }
select option:first-child { color: grey; }
select.empty { color: grey; }
/* Hidden placeholder */
select option[disabled]:first-child { display: none; }

And a little jQuery to toggle the font-color after selection:

// Applies to all select boxes that have no value for their first option
$("select:has(option[value=]:first-child)").on('change', function() {
    $(this).toggleClass("empty", $.inArray($(this).val(), ['', null]) >= 0);
}).trigger('change');

Inspiration:
https://stackoverflow.com/a/5805194/1196148 - re-selectable placeholder
https://stackoverflow.com/a/8442831/1196148 - hidden placeholder

久伴你 2024-12-26 11:50:33

我按照@Truth建议构建了一个简单的演示: http://jsfiddle.net/6QnXF/

(function($){
    $('#myAwesomeSelect').change(function(){
       if( !$(this).data('removedPlaceHolder'))
       {
          $(this).find('option:first').remove();
          $(this).data('removedPlaceHolder', true); 
       }
    });
})(jQuery);

我希望它对你有用。

I've built a simple demo by following @Truth suggestion: http://jsfiddle.net/6QnXF/

(function($){
    $('#myAwesomeSelect').change(function(){
       if( !$(this).data('removedPlaceHolder'))
       {
          $(this).find('option:first').remove();
          $(this).data('removedPlaceHolder', true); 
       }
    });
})(jQuery);

I hope it works for you.

无妨# 2024-12-26 11:50:33

据我所知,单独使用 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.

凹づ凸ル 2024-12-26 11:50:33

我的猜测是,您将必须为选择框构建自己的解决方法。类似一系列充当选项的 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.

土豪 2024-12-26 11:50:33

试试这个:
中添加:

select:required:invalid {
 color: #c4c4c4;
}
option[value=""][disabled] {
 display: none;
}
option {
 color: #232423;
}

在 css(样式表):和 HTML

<select required>
 <option value="" disabled selected>Select a color</option>
 <option value="red">Red</option>
 <option value="green">Green</option>

try this:
add this in css(style sheet):

select:required:invalid {
 color: #c4c4c4;
}
option[value=""][disabled] {
 display: none;
}
option {
 color: #232423;
}

and the HTML:

<select required>
 <option value="" disabled selected>Select a color</option>
 <option value="red">Red</option>
 <option value="green">Green</option>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文