简体   繁体   English

在动态创建的文本框中更改文本

[英]Change text in dynamically created textbox

I have a bunch of textboxes that are created dynamically one per click. 我有一堆文本框,它们是每次单击动态创建的。 They all have the same name "discount[]", same id "discount" and have the same class "tinput". 它们都具有相同的名称“ discount []”,相同的标识“ discount”和相同的类“ tinput”。

Is there anyway that I can use to change the text in all of these textboxes with javascript or jquery? 无论如何,我可以使用javascript或jquery来更改所有这些文本框中的文本吗?

I have another bunch of the dynamically created text boxes. 我还有一堆动态创建的文本框。 The id and name are unique but the class remains the same. id和名称是唯一的,但类保持不变。 If it gets the job done, I can change the class name. 如果完成工作,我可以更改类名。

Anyhelp is greatly appreciated. 任何帮助是极大的赞赏。

Regards, 问候,

You definitely shouldn't use an id more than once per document. 每个文档的ID绝对不要超过一次。 If you need it for a <label> you might want to consider putting the input in the <label> . 如果需要它作为<label> ,则可能需要考虑将输入内容放入<label>

You are using the class attribute the right way, though. 但是,您正在使用正确的class属性。 You can do: 你可以做:

$('.tinput').val('New value for all .tinputs!');

If you can't put the input inside the label you can use use this. 如果您不能将输入内容放在标签内,则可以使用此功能。 I made this demo for someone a little while ago. 不久前,我为某人制作了此演示。 It might be helpful. 这可能会有所帮助。 It'll increment an id like "id-1". 它将像“ id-1”一样增加一个id。

http://jsbin.com/APegOMo/3/edit?html,js,output http://jsbin.com/APegOMo/3/edit?html,js,输出

same id "discount" 相同的ID“折扣”

Please, don't do that! 拜托,不要那样做! "id" is to identify a dom element. “ id”是标识dom元素。 That hurts in my soul. 那伤了我的心灵。 If every id is unique you could access them by $("#myid").val("my new val"). 如果每个ID都是唯一的,则可以通过$(“#myid”)。val(“ my new val”)访问它们。 Just an example. 只是一个例子。

Regards, - Tobbo 问候-Tobbo

here is a simple example that takes all elements with a particular class name and changes the .html, not 100% sure I understand what you're after though. 这是一个简单的示例,该示例接受具有特定类名的所有元素并更改.html,但并不是100%肯定我理解您的要求。 if you need to do someting specific with each divs text you can iterate through them by id (and maybe class as well) and change each divs individual .html. 如果您需要对每个div文本进行特定的操作,则可以按ID(可能还有类)遍历它们,并分别更改每个div .html。

if you can share a stripped down example of your code or elaborate I'd love to try to help. 如果您可以分享代码的精简示例或详尽说明,我很乐意为您提供帮助。 good luck. 祝好运。

$(".changeDivs").html("adfasdf");

jsfiddle example >> jsfiddle示例>>

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

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