在javascript中访问转发器控件. [英] Accessing repeater controls in javascript.

查看:62
本文介绍了在javascript中访问转发器控件.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我需要一些帮助.我正在使用VS2008.

我有一个具有5列的转发器控件.前4列包含4个文本框,最后一列是一个按钮.现在,我想对4个文本框进行验证,每当我单击按钮时,它都应调用javascript以检查4个文本框的总和是否恰好是100.

现在我的问题是,在javascript函数中,如何才能一个接一个地访问转发器文本框",这样我就可以总计它们并相应地显示警报消息..


快速帮助将不胜感激.
问候,

Shikhar

Hello People,

I need some help.I am using VS2008.

I have a repeater control which has 5 columns . First 4 columns contains 4 textboxes and last column is a button. Now i want to put a validation on 4 textboxes that whenever i click on the button, it should call javascript to check if the sum of 4 textbox should exactly be 100.

Now my prob is that in the javascript function, how can i "access the repeater textboxes" one by one so that i can total them and show an alert message accoringly..


Quick help would be appreciated.
Regards,

Shikhar

推荐答案

你好,

我有2个简单的解决方案

1.使用通常的Js函数"getElementsByTagName",这将带给我们页面中的所有输入控件,要获得更准确的结果,您可能需要根据其类型或其他属性来过滤它们,请检查以下内容

函数ValidateMe(){
var AllTxtBoxes =新数组;

AllTxtBoxes = document.getElementsByTagName("input");
for(var i = 0; i< = AllTxtBoxes.length-1; i ++)
{if(AllTxtBoxes [i] .type =="text")
//做你的工作
;
}
}

< asp:Repeater ID ="Repeater1" runat ="server">
< ItemTemplate>
< span id ="label" runat ="server"></span>
< input type ="text" runat ="server" onchange ="ValidateMe();" />
</ItemTemplate>
</asp:Repeater>

以防万一这些是页面中唯一的文本框.

2.我认为此解决方案更干净,使用jQuery可以先通过标记名获取它们,然后在这种情况下通过具有特定属性可以添加自己的自定义属性,然后在Js中读取它,请检查以下内容

您的文本框将更新为
< input type ="text" runat ="server" onchange ="ValidateMe();" MyCustomAttr ="1"/>

在ValidateMe函数中,您将需要一行代码
AllTxtBoxes = jQuery("input [MyCustomAttr]");


希望这很有价值,祝你好运! :)
Hello,

I have 2 simple solutions

1. using the usual Js function "getElementsByTagName" this will bring us all the input controls in the page , for a more accurate result you might need to filter them based on their type or maybe any other attribute , check the below

function ValidateMe() {
var AllTxtBoxes = new Array;

AllTxtBoxes = document.getElementsByTagName("input");
for (var i = 0; i <= AllTxtBoxes.length - 1; i++)
{if (AllTxtBoxes[i].type == "text")
//do your work
;
}
}

<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<span id="label" runat="server"></span>
<input type="text" runat="server" onchange="ValidateMe();" />
</ItemTemplate>
</asp:Repeater>

this is in case these are the only text boxes in the page .

2. i think this solution is cleaner , using jQuery you can get them first by tag name , and then by having a specific attribute in this case you can add your own custom attribute and then read it in Js , check the below

your text box will be updated to
<input type="text" runat="server" onchange="ValidateMe();" MyCustomAttr="1" />

and in the ValidateMe function you will need one line of code
AllTxtBoxes = jQuery("input[MyCustomAttr]");


Hope this was of a value , good Luck ! :)


这篇关于在javascript中访问转发器控件.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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