如何避免动态生成的文本框中的空间 [英] how to avoid space in dynamic generated textboxes

查看:47
本文介绍了如何避免动态生成的文本框中的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有动态生成的文本框。我编写代码来检查文本框是否为空,然后应该显示一条消息。但它只验证了第一个动态文本框,它也占用了空间。怎么做 。请帮助。

if(document.getElementById(txt)。value ==){



alert(请输入序列号);



document.getElementById(txt)。focus();

返回false;

Hi everyone,
I Have dynamic generated textboxes. I wrote code to check if the textboxes empty then should show a message. but it is validating only first dynamic textbox and it is also taking space. how to do it . kindly help.
if (document.getElementById("txt").value == "") {

alert("Please enter Serial Number");

document.getElementById("txt").focus();
return false;

推荐答案

如果有人可以定义多个具有相同ID的元素,则getElementById()方法返回第一个元素。对于多个ID调用,你可以使用getElementsByName ..并使用循环。



PlaceHolder1.Controls.Add(new LiteralControl(< input id ='txt'name = '文本框'+ i +'type ='text'/>));



u需要为所有动态文本框更改这样的代码



PlaceHolder1.Controls.Add(new LiteralControl(< input name ='txt'id ='Textbox+ i +'type ='text'/> ));





然后调用你的getElementByName并使用这样的循环







var elemArray = document.getElementsByName(name);

for(var i = 0; i< elemArray。长度; i ++){





- 记下您的代码以检查序列号。与

document.getElementById(elemArray [i] .id)



}





我没有给你全部代码bt给你提示。所以你可以试试这个,也许对你有帮助。
if anyone can define more than one element with the same ID , the getElementById() method returns the first element. for multiple ID calling you can use getElementsByName.. and use loop.

PlaceHolder1.Controls.Add(new LiteralControl("<input id='txt' name='Textbox" + i + "'type='text' />"));

u need to change your code like this for all your dynamic textbox

PlaceHolder1.Controls.Add(new LiteralControl("<input name='txt' id='Textbox" + i + "'type='text' />"));


and then call your getElementByName and using loop like this



var elemArray = document.getElementsByName(name);
for(var i = 0; i < elemArray.length; i++){


- write down your code for checking serial number. with
document.getElementById(elemArray[i].id)

}


I m not giving you whole code bt give you hint. so u can try with this and may be it will helpful to you.


这篇关于如何避免动态生成的文本框中的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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