简体   繁体   English

如何使用javascript定位动态生成的文本框

[英]How to target dynamic generated text boxes using javascript

I have a TextBox that gets generated in a GridView dynamically. 我有一个动态地在GridView生成的TextBox When you inspect the page the TextBox looks like this below. 当您检查页面时, TextBox如下所示。 I am trying to find a way to target this TextBox and all other TextBoxes in this GridView so that i can run a script against them. 我试图找到一种方式来瞄准这一TextBox和所有其他TextBoxes在此GridView ,这样我可以对他们运行一个脚本。 I am having a lot of trouble finding a way of being able to target this text box. 我找到一种能够定位此文本框的方法很困难。 I tried setting a style class to the GridView and call on that but it doesn't seem to be working. 我尝试将一个样式类设置为GridView并调用它,但它似乎没有工作。

<input name="ctl00$MainContent$controlProductsList1$gvItems$ctl04$Option-30482_e0edc10a-7dba-40d0-a4f1-7c4c5801c0ca" type="text" id="MainContent_controlProductsList1_gvItems_Option-30482_e0edc10a-7dba-40d0-a4f1-7c4c5801c0ca_2">

What is the best way using javascript to target the TextBoxes that are dynamically generated in a GridView? 使用javascript来定位在GridView中动态生成的TextBox的最佳方法是什么?

Can you give the gridview a class and call on that class alone? 你能给gridview一个班级并单独打电话给那个班级吗?

This is the script that will need to target the inputs 这是需要定位输入的脚本

$('#inputID').keyup(function(){
    this.value = this.value.toUpperCase();
});

One of the idea could be put a css class on the textbox. 其中一个想法可能是在文本框上放置一个css类。 If it is generated by your code dynamically. 如果它是由您的代码动态生成的。
And then use class selector in your jQuery. 然后在jQuery中使用类选择器。

  $('.inputcss').keyup(function(){
               this.value = this.value.toUpperCase();
   });

Or even you can use id of the gridview and find input:text inside the gridview and use your function. 甚至你可以使用gridview的id并在gridview中找到input:text并使用你的函数。 Below is a sample code 下面是一个示例代码

 $('#<%=grid.ClientID%>')
 .find('tr')
 .each(function(row) {
      $(this).find('input')
            .each(function(col) {
                      $(this).keyup(function(){
               this.value = this.value.toUpperCase();
                  });

                  } 
             });
  });

}); 

Edit 1 编辑1

And if you only want to show in upper case you can use css as follows 如果你只想以大写字母显示,你可以使用如下css

 h1 {text-transform:uppercase;}

More details 更多细节

http://www.w3schools.com/cssref/pr_text_text-transform.asp http://www.w3schools.com/cssref/pr_text_text-transform.asp

Since you appear to be using jQuery, check out your options on the JQuery selector page. 由于您似乎正在使用jQuery,请查看JQuery选择器页面上的选项。 One option is to use a generic input selector like: 一种选择是使用通用输入选择器,如:

$("#<%=gvItems.ClientID%> :input").keyup(function(){
      this.value = this.value.toUpperCase();
});

The code above assumes your control is called gvItems and will render the client ID of the grid view for use in the JQuery ID selector (#MainContent_controlProductsList1_gvItems...) and find all input child elements (:input). 上面的代码假设您的控件名为gvItems,并将呈现网格视图的客户端ID以供在JQuery ID选择器中使用(#MainContent_controlProductsList1_gvItems ...)并查找所有输入子元素(:input)。

Another option would be to put a class directly on the input controls: 另一种选择是将类直接放在输入控件上:

$(".inputClass").keyup(function(){
    this.value = this.value.toUpperCase();
});

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

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