防止用户在下拉菜单中选择默认值 [英] Prevent user from picking the default value in a dropdown menu

查看:81
本文介绍了防止用户在下拉菜单中选择默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图阻止用户选择默认的下拉菜单选项.有什么方法可以在下拉菜单选项中添加文本而不是值?我的代码:

I am trying to prevent the user from picking the default dropdown menu option. Is there any way to add text instead of value in the dropdown menu option? My code:

<select name='test'>
<option value='default' selected='selected'>Please select the value</option>
<option value='value1'>value1</option>
<option value='value2'>value2</option>
</select>

我不希望用户选择请选择值"选项.有更好的方法吗?

I don't want the user to pick 'Please select the value' option. Is there better way to to this?

推荐答案

客户端代码(javascript/HTML)可以满足要求,但这只是成功的一半.

Client side code (javascript/HTML) is OK for this, but it's only half the battle.

由于您不希望用户实际选择此默认值",因此我将这样做:

Since you don't want the user to actually select this "default value", this is what I would do:

<option value=''>Please select the value</option>

value保留为空字符串.然后在服务器端代码中,检查发布的值是否为空.如果您不打算使用默认值,则填充它是没有意义的.

Leve the value as an empty string. Then in your server side code, check if the value posted was empty or not. There's no point in populating an arbitrary default value if you aren't going to use it.

默认情况下,将根据标准浏览器的行为选择第一个<option>,因此您实际上并不需要selected属性.您确实需要value='',否则浏览器可以发布<option>的文本内容.

By default, the first <option> will be selected according to standard browser behavior, so you don't really need theselected attribute. You do need the value='' though or the browser can post the text content of the <option>.

这篇关于防止用户在下拉菜单中选择默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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