简体   繁体   English

如何区分文本框

[英]how to differentiate text boxes

sorry for weird question... 抱歉奇怪的问题......

so i want to alert (testing for now) values of text boxes using javascipt and php and right now, i have loops creating various text boxes with the same name. 所以我想使用javascipt和php警告(现在测试)文本框的值,现在,我有循环创建具有相同名称的各种文本框。 when i try to alert a text box with a name, it chooses only the first one... i'm not sure how to deal whit this. 当我尝试使用名称警告文本框时,它只选择第一个...我不知道如何处理这个问题。 is there a way to do this with forms or div? 有没有办法用表格或div做到这一点? heres some code... 继承了一些代码......

function insertComment(comment){
     alert(comment);
}

//say... i have a for loop that makes 5 of these 
echo "<textarea rows='3' cols='20' name='txt_comment' id = 'txt_comment'>Enter comment here...</textarea>" . "</br>";
echo "<button onClick='insertComment(txt_comment.value)'>Submit Comment</button>";

thanks! 谢谢!

不确定,但应该使用.each()与jquery,这将帮助您创建所选元素的循环...

give each text box a same class say "text-boxes" and then use jquery method each 给每个文本框一个相同的类说“文本框”,然后分别使用jquery方法

$("input.text-boxes").each(...);

or try giving diff id to each textbox if using a loop like 或者如果使用类似的循环,尝试为每个文本框提供diff id

loop for $i
"text-comment" . $i

and in js function loop with the same thing 并在js函数循环中使用相同的东西

for i = 1 to 5
  ..document.getElementById("text-comment" + i).value..

您可以使用for循环索引为文本框指定不同的id ,并按id检索require字段。

Quote from w3schools : 来自w3schools的报价:

The id attribute specifies a unique id for an HTML element (the id attribute value must be unique within the HTML document). id属性指定HTML元素的唯一ID(id属性值在HTML文档中必须是唯一的)。

So you shouldn't have 5 textareas with same id. 所以你不应该有5个具有相同id的textareas。 One solution is to give different ids to textareas. 一种解决方案是给textareas提供不同的id。
Other solution is using getElementsByTagName('textarea') and then loop inside the node list. 其他解决方案是使用getElementsByTagName('textarea')然后在节点列表中循环。
You can also use getElementsByClassName if you assign a class to your textareas. 如果为textareas分配类,也可以使用getElementsByClassName

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

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