JQUERY:按控件 ID 查找

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5370716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:02:54  来源:igfitidea点击:

JQUERY: Finding by control ID

jqueryasp.net

提问by OrElse

Currently my aspx page contains

目前我的 aspx 页面包含

<input type="text" name="openid_username" />
<input type="text" name="openid_identifier" />

but now i would like to replace them with

但现在我想用

<asp:TextBox ID="openid_username" runat="server"></asp:TextBox>
<asp:TextBox ID="openid_identifier" runat="server"></asp:TextBox>

so how should i modify the following JQUERY, to reflect the input boxes to textboxes replacement?

那么我应该如何修改以下 JQUERY,以将输入框反映到文本框替换?

  var $usr = $this.find('input[name=openid_username]');
  var $id = $this.find('input[name=openid_identifier]');

回答by tvanfosson

I would use the "ends with" option on the attribute selector in case the name is mangled by the control being in a container. Note the $=instead of =.

我会在属性选择器上使用“ends with”选项,以防名称被容器中的控件破坏。请注意$=代替=

var $usr = $this.find('input[name$=openid_username]');
var $id = $this.find('input[name$=openid_identifier]');

回答by C???

If you want the exact ID of your controls, you can do this:

如果您想要控件的确切 ID,可以执行以下操作:

var $usr = $this.find('#<%= openid_username.ClientID %>');
var $id = $this.find('#<%= openid_identifier.ClientID %>');

Of course, this is assuming your JS isn't in an external file.

当然,这是假设您的 JS 不在外部文件中。

回答by Hogan

 var $usr=$("#openid_username");

should work or try

应该工作或尝试

 var $usr = $('[id*=openid_username]');

回答by Rotem Tamir

I'm not sure but if ID translates into the elements ID attribute (i.e turns into <input type="text" id="openid_username">in the aspx generated html, I would go with:

我不确定,但如果 ID 转换为元素 ID 属性(即<input type="text" id="openid_username">在 aspx 生成的 html 中转换,我会选择:

var $usr = $("#openid_username");

回答by moe

var $usr = $this.find('#openid_username');
var $id = $this.find('#openid_identifier');

回答by Mahesh Velaga

I would go with the following to achieve that:

我会采取以下措施来实现这一目标:

var $usr = $("input[id$='openid_username']");
var $id = $("input[id$='openid_identifier']");