在ASP.NET中使用JQuery选择由ID属性 [英] Selecting by ID attribute using JQuery in ASP.NET

查看:246
本文介绍了在ASP.NET中使用JQuery选择由ID属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始在VS 2008中使用JQuery,到目前为止,我喜欢它!但是,我很困惑如何,我应该以选择网页上的asp.net控制来使用JQuery。

I've just started using JQuery in VS 2008, and so far I like it! But, I'm confused about how I should be using JQuery in order to select asp.net controls on a webpage.

例如,我有以下code(只是一个实物模型):

For example, I have the following code (just a mock-up):

<asp:textbox id="txtSomeData1" runat="server" text="Some Data!!"></textbox>

现在,如果我想使用jQuery选择文本框,并改变它的文本为更多的数据!,那我就必须做一些这样的:

Now, if I want to use JQuery to select the textbox and change it's text to "Some More Data!!", then I would have to do something like:

$('input#ctl00_ContentPlaceHolder1_txtSomeData1').val('Some More Data!!');

其中,坦率地说,是烦人,因为我不想惹不得不找出控件的ID是什么它呈现到网页后(ctl00_ContextPlaceHolder ......等等等等等等)。

Which, quite frankly, is annoying because I don't want to mess with having to figure out what the id of the control is after it's rendered to the webpage (ctl00_ContextPlaceHolder... blah blah blah).

有没有我可以选择文本框,而不必使用它的ID的方法是什么?另外,我知道,你可以通过类名称选择,但没有太多帮助,如果您要选择的控制没有一个类。

Is there a way that I can select the textbox without having to use the id of it? Also, I know that you can select by class name, but that doesn't help much if the control that you're selecting doesn't have a class.

我是不是失去了一些东西在这里?

Am I just missing something here?

JUST重申:我不想使用一个类来选择输入标签!我想用的IDtxtSomeData1,而不是被渲染到网页中的长ID。

推荐答案

您想要做的可以是:

$("input[id$='_txtSomeData1']").val().....

或者你可以添加一个类或自定义属性,你可以选择文本框

or you could add a class or custom attribute to the textbox that you can select on

这篇关于在ASP.NET中使用JQuery选择由ID属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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