是否可以在asp.net下拉列表中添加占位符 [英] Is it possible to add placeholder in an asp.net Dropdownlist

查看:120
本文介绍了是否可以在asp.net下拉列表中添加占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在asp.net Dropdownlist中添加占位符

Is it possible to add placeholder in an asp.net Dropdownlist

推荐答案

根据 Mozilla Dev Network [ ^ ],占位符不是输入的有效属性。此外,建议使用值属性,以防某些旧版浏览器依赖它。



相反,添加一个空值的选项,如下所示:



< select placeholder =选择你的饮料>

< option value =default =selected =>选择你的饮料< / option>

< option value =tea> Tea< / option>

< option value =coffee> Coffee< / option>

< option value =soda> Soda< / option>

< / select>



额外奖励:在较新的浏览器中,如果选择了默认选项,则向元素添加必需属性将不允许用户提交表单。



如果要设置默认选项的样式,则需要以下CSS:



According to Mozilla Dev Network[^], placeholder is not a valid attribute on a input. And also, a value attribute is suggested, just in case some older browsers depend on it.

Instead, add an option with an empty value like so:

<select placeholder="select your beverage">
<option value="" default="" selected="">select your beverage</option>
<option value="tea">Tea</option>
<option value="coffee">Coffee</option>
<option value="soda">Soda</option>
</select>

Bonus: In newer browsers, adding the required attribute to the element will not allow the user to submit the form if the default option is selected.

If you want to style the default option, you need the following CSS:

select option[default] {
    color: #333;
}



请注意,这只会调整drop内的项目的样式 - 下拉列表,而不是输入上显示的值。如果你想使用CSS直接定位你的元素。



如果你想让用户稍微选择默认项目,你可以使用display:none; CSS规则,但请记住,这不会阻止用户选择它,这只会使他们更难以这样做。


Note that this will only style the item inside the drop-down list, not the value displayed on the input. If you want to style that with CSS target your element directly.

If you wanted to make it slightly harder for the user to select the default item, you could use a display: none; CSS rule, but remember that this will not prevent users from selecting it, this just makes it harder for them to do so.


这篇关于是否可以在asp.net下拉列表中添加占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆