English 中文(简体)
清单Box中基于案文的选择性清单
原标题:Select ListItem in ListBox based on Text from TextBox

I am trying to select an item in a ListBox based on text entered in a textbox using jquery. If the length of the text entered in the textbox is greater than 1 I would like to loop through the items in the ListBox and compare the value of each item and if it matches the numbers entered in the textbox I need to select/highlight it in the ListBox. Here is what I am doing but doesn t seem to work. The ListBox ListItem s are populated at runtime from the database.

Asp.Net

<asp:TextBox ID="txtMediaCode" runat="server" MaxLength="2" Width="40px" />
<asp:ListBox ID="lsMediaCodes" runat="server" Width="296px" />

Jquery

<script type="text/javascript">

    $(document).ready(function () {

        $( #txtMediaCode ).keyup(function () {
            if ($( #txtMediaCode ).length > 1) {
                $( #lsMediaCodes ).each(function (i, option) {

                    if ($(option).val() == $( #txtMediaCode ).val()) {
                        $(option).attr( selected ,  selected );
                    }

                });
            }
        });

    });

</script>

是否有任何建议?

最佳回答

Try -

$(document).ready(function() {
    $( #txtMediaCode ).keyup(function() {
        console.log( in )
        if ($( #txtMediaCode ).val().length > 1) {
            $( #lsMediaCodes > option ).each(function(i, option) {
                if ($(option).val() == $( #txtMediaCode ).val()) {
                    $(option).attr( selected ,  selected );
                }
            });
        }
    });
});

这是你的法典,同时做了一些改动:

  • It now checks the length of the value in #txtMediaCode textbox rather than how many elements are on the page
  • The each selector has changed to $( #lsMediaCodes > option ).each( this will select all option elements rather than just the list itself

Demo -

问题回答

类似情况:

selectMediaCode ($("#<%txtMediaCode.ClientID%>").val());

selectMediaCode = function(text){    
    $("#<%=lsMediaCodes.ClientID%> option:contains( " + text + " )").attr("selected", "selected");
}

你也可以这样做:

selectMediaCode = function(text){     
    $("#<%=lsMediaCodes.ClientID%> option[value= " + text + " ]").attr("selected", "selected"); 
} 

http://www.ohchr.org。

我认为你甚至可以这样做:

selectMediaCode = function(text){     
    $("#<%=lsMediaCodes.ClientID%>").val(text);
} 

你们需要做一些事情才能完成这项工作。 首先,你们需要为任何伙伴关系使用一种像以下的挑选器。 NET控制:

$( #<%= lsMediaCodes.ClientID %> )

当对蚊帐进行控制时,有与你对蚊帐的控制截然不同的身份证。

接下来,你需要研究清单Box生成的哪些html要素,以保存每个清单项目,而不是选择清单Box。

当你创建协会时。 该网络在你的项目中控制不构成伙伴关系。 NET,成为超文本。 既然如此,你必须说明每一项控制的结果,就像你想要用javascript/jQuery与其合作一样。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签