javascript - How to set the value in the textboxes - asp.net using jquery? - Stack Overflow

I have a table with id 'tbl' which contains textbox controls with id's like below<tab

I have a table with id 'tbl' which contains textbox controls with id's like below

<table id="tbl">

txt1Text1
txt1Text2

txt2Text1
txt2Text2

txt3Text1
txt3Text2
.................
.................

I want to set the specif value in the textboxes that have id ends with Text1 I want to do it using jquery/javascript.

Thanks for help.

I have a table with id 'tbl' which contains textbox controls with id's like below

<table id="tbl">

txt1Text1
txt1Text2

txt2Text1
txt2Text2

txt3Text1
txt3Text2
.................
.................

I want to set the specif value in the textboxes that have id ends with Text1 I want to do it using jquery/javascript.

Thanks for help.

Share Improve this question edited Sep 25, 2012 at 12:56 Muhammad Akhtar 52.2k37 gold badges139 silver badges191 bronze badges asked Sep 25, 2012 at 12:50 Azeem Raza TayyabAzeem Raza Tayyab 631 silver badge6 bronze badges 5
  • please specify whether the control is a html control or server control – cc4re Commented Sep 25, 2012 at 12:51
  • Please note one point, he want to set the value of only those controls, that have id ends with 'Text1'. In question, there are 3 controls. – Muhammad Akhtar Commented Sep 25, 2012 at 12:55
  • You should add a fake css class, that allows you to "tag" the textboxes. Then use jQuery to find these textbox using the css class selector. In fact, you should also descrive how you create the textboxes. One by one? using a databound control? using mvc? created on the fly? With the answer to this question, you will get far more better answers as there are probably thousands way to solve the issue. – Steve B Commented Sep 25, 2012 at 12:56
  • @SteveB, post that answer and I'll upvote it. That seems to be the "best practice" answer. – Neil Commented Sep 25, 2012 at 12:58
  • @Neil: I will when azeemraza will give more details on how he creates the textboxes. – Steve B Commented Sep 25, 2012 at 13:01
Add a ment  | 

5 Answers 5

Reset to default 5

You can use Attribute Ends With selector.

$('#tbl input[type=text][id$=Text1]').val('new value')

You should add a fake css class, that allows you to "tag" the textboxes, then use jQuery to find these textbox using the css class selector.

<asp:TextBox runat="Server" CssClass="existingClass FakeClass" id="txt1" />
<asp:TextBox runat="Server" CssClass="existingClass FakeClass" id="txt2" />
<asp:TextBox runat="Server" CssClass="existingClass FakeClass" id="txt3" />

<script type="text/javascript">
    $(function(){
        $(".FakeClass").val("42");
    });
</script>

What is important here, is that the "FakeClass" does not have to exists. It's only a marker.

$("input[id $= Text1]").val('your value');

Try with this

$('#tbl input[id$="Text1"]').val('my value');

Attribute Ends With Selector

Selects elements that have the specified attribute with a value ending exactly with a given string. The parison is case sensitive.

$('input[type=text][id$=Text1]').val('value');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745446598a4628075.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信