简体   繁体   English

asp.net下拉列表始终选择带回发的0索引

[英]asp.net dropdown list always select 0 index with postback

working on a simple project after click a button i'm clear all textbox text and I want dropdown list select it's 0 index value..........I'm trying to write some jquery code to complete this work 单击一个按钮后,在一个简单的项目上工作我清除了所有文本框文本,并且希望下拉列表选择它的0索引值..........我正在尝试编写一些jquery代码来完成这项工作

  $(document).ready(function () {
            $("#btnAdd").bind("click", function () {
                $("#productNameDDL")[0].selectedIndex = 0;
            });
        });

but don't work this code what should have I do now.Thanks in advanced 但不要使用此代码,现在我应该怎么办。感谢进阶

Clear the form instead, if you can't then best solution will be 相反,请清除表单,如果不能,则最好的解决方案是

$("#target").val($("#target option:first").val());

Or you can use 或者你可以使用

$('select option[value="your_value"]').attr("selected",true); .

If you want to select option by content 如果要按内容选择选项

$('select option:contains("it\'s me")').prop('selected',true);

You need to use the :contains(text) selector to find via the containing text. 您需要使用:contains(text)选择器通过包含的文本进行查找。

Also jQuery prop offeres better support for Internet Explorer when getting and setting attributes. 当获取和设置属性时,jQuery prop还为Internet Explorer提供了更好的支持。

Since you tagged your question with asp.net, the problem is most likely the id of the DropDownList. 由于您使用asp.net标记了问题,因此问题很可能是DropDownList的id Use: 采用:

$("#<%= productNameDDL.ClientID %>").prop('selectedIndex', 0);

Asp.net renames the ID propery of a Control on the client side to ensure unqiue id's. Asp.net在客户端重命名控件的ID属性,以确保ID不受限制。 See the Microsoft Site for more info. 有关更多信息,请参见Microsoft网站

UPDATE 更新

To change the SelectedIndex of a DropDownList programatically you can use this. 要以编程方式更改DropDownList的SelectedIndex,可以使用此方法。

protected void Button1_Click(object sender, EventArgs e)
{
    productNameDDL.SelectedIndex = 0;
    //or
    productNameDDL.SelectedValue = "value";
}

You basically want to use "first" and target the elements attribute 您基本上想使用“第一”并定位elements属性

 $(document).ready(function () { $("#btnAdd").bind("click", function () { $("#productNameDDL option:first").attr('selected','selected'); }); }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM