如何自动删除下拉列表的选择 [英] how to remove automatically selection of dropdownlist

查看:154
本文介绍了如何自动删除下拉列表的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,按钮和一个下拉列表.

I have a textbox,button and a dropdownlist.

dropdownlist item
select the item
card1
card2
card3


我单击未选择项目的按钮,选择项目"被自动选择.我不想要它.


I click the button without selection of item,"select the item" it is selected automatically.I do not want it.

推荐答案

我认为您希望用户必须选择任何除select the item以外的其他选项.
为此,您需要从客户端/服务器端验证DropDownList.

客户端验证
I think you want user must select any of the options except select the item.
for that you need to validate your DropDownList from client side/server side.

client side validation
<script>
var dropdownvalue=document.getElementById("ClientIDofYourDropDownList").value;
if(dropdownvalue=="select the item")
{
alert("You need to select any of the card");
document.getElementById("ClientIDofYourDropDownList").focus();
return;
}
</script>


在这里,我们强迫用户选择DropDownList的一个选项.

服务器端验证


here we are forcing the user to select one option of the DropDownList.

Server side validation

if (IDofYourDropDownList.SelectedValue == "select the item")
        {
            Label.Text = "You need to select any of the card";
            return;
        }





Hop it helps.


默认情况下,下拉列表选择列表中的第一项(在所有浏览器中),这就是您(我们)将第一项用作选择项目",以通知用户他需要选择一个项目并为我们提供一种验证输入的方法.列表中的第一项是虚拟"项目,它不代表该列表的任何内容,而只是为您提供一种方法,让您知道用户是否选择了对您有意义的任何内容.

每个人都在做的事情是检查列表中的所选项目是否与虚拟"项目不同,这样我们就知道输入对于该列表是有效的,我们可以继续.

例如:
下拉列表:
-asp.net控件:
By default a dropdown list is selecting the first item in the list (in all browsers), that''s why you (we) are using the first item as "select the item" to notice the user that he needs to select an item and to give us a way of validating the input. The first item from your list is a "dummy" item that does not represent anything for the list, but just offers you a way to know if the user selected anything meaningful for you.

What everyone is doing is checking if the selected item of the list is different from the "dummy" item, that way we know the input is valid for that list and we can continue.

eg:
The drop down list:
- as asp.net control:
<asp:dropdownlist id="ddlItems" runat="server" xmlns:asp="#unknown">
    <asp:listitem value="-1" text="select the item"></asp:listitem>
    <asp:listitem value="1" text="card 1"></asp:listitem>
    <asp:listitem value="2" text="card 2"></asp:listitem>
    <asp:listitem value="3" text="card 3"></asp:listitem>
</asp:dropdownlist>


-或作为简单的html控件:


- or as simple html control:

<select id="ddlItems" runat="server">
    <option value="-1">select the item</option>
    <option value="1">card 1</option>
    <option value="2">card 2</option>
    <option value="3">card 3</option>

</select>



按钮上的条件可能是这样的:



and the condition on your button could be something like this:

if (ddlItems.SelectedValue == "-1")
{
    //you selected "select the item" option - the "dummy" element
    //do something here
}



而且,如果您仍然不希望像您所说的那样,则可能需要将其从列表中删除.



And if you still do not want it as you said, than maybe you need to remove it from the list.


这篇关于如何自动删除下拉列表的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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