[英]How can I update the background color of a dynamic input control?
I'm successfully creating some dynamic input textboxes using the following javascript: 我使用以下javascript成功创建了一些动态输入文本框:
var type = "Textbox";
var foo = document.getElementById("fooBar");
for (i = 1; i <= totalQty; i = i + 1) {
var textbox = document.createElement("input");
//Assign different attributes to the element.
textbox.setAttribute("type", type + i);
//textbox.setAttribute("value", type + i);
textbox.setAttribute("name", type + i);
textbox.setAttribute("id", type + i);
textbox.setAttribute("style", "width:300px");
textbox.setAttribute("width", "300px");
//Append the element in page (in span).
var newline = document.createElement("br");
foo.appendChild(newline);
foo.appendChild(textbox);
}
Everything works fine with that. 一切都很好。 Once the user keys in data and clicks submit however, I need to go back and set the background-color of any textboxes with an error to red.
一旦用户键入数据并点击提交,我需要返回并将任何文本框的背景颜色设置为红色错误。 I found some code to do the actual coloring:
我发现了一些代码来进行实际着色:
textbox.style.backgroundColor = "#fa6767";
...and I know the exact name of the textbox with the error (ie "Textbox1", "Textbox2", "Textbox3", etc) but I'm not sure how to programatically assign this background color code to the specific textbox. ...我知道带有错误的文本框的确切名称(即“Textbox1”,“Textbox2”,“Textbox3”等)但我不确定如何以编程方式将此背景颜色代码分配给特定文本框。 I can't use something like this, since all code is dynamically generated:
我不能使用这样的东西,因为所有代码都是动态生成的:
errorTextbox = $("#Textbox1");
Any suggestions? 有什么建议么?
It looks like you're building a form validation script. 看起来您正在构建表单验证脚本。 Here's an easier way to do this:
这是一种更简单的方法:
1) Create an entry in your stlyesheet for your error class. 1)在您的stlyesheet中为您的错误类创建一个条目。 Adding and removing a class requires fewer steps than assigning properties individually.
添加和删除类比单独分配属性所需的步骤更少。
.error {
background-color: #ff0000;
}
2) Give all the textboxes you wish to validate a unique class name "valMe", for example. 2)例如,给你想要验证的所有文本框一个唯一的类名“valMe”。
3) Then loop through them during the validation step: 3)然后在验证步骤中循环遍历它们:
$('.valMe').each(function() {
$(this).removeClass('error');
if($(this).text=='') {
$(this).addClass('error');
}
})
By using "this" you refer to the current element, so you don't even need to know the ID of the element. 通过使用“this”,您可以引用当前元素,因此您甚至不需要知道元素的ID。
If you already know the name
(in this case identical to the id
) of the element, you can use jQuery to select the element by forming the selector using string concatenation. 如果您已经知道元素的
name
(在这种情况下与id
相同),则可以使用jQuery通过使用字符串连接形成选择器来选择元素。 Assuming you have a variable that stores the name
/ id
of the text box that has the error, then it's a relatively simple process: 假设你有一个变量来存储有错误的文本框的
name
/ id
,那么这是一个相对简单的过程:
var errorTextboxName = 'Textbox1';
$('#' + errorTextboxName).css('background-color', 'red');
I ended up going with the following: 我最终得到了以下内容:
document.getElementById('Textbox1'.style.backgroundColor = "#fa6767";
I originally didn't think I would be able to capture my "Textbox1" control in this fashion since when I viewed the html source code, there was no "Textbox1" due to the fact I dynamically created it. 我原本不认为我能够以这种方式捕获我的“Textbox1”控件,因为当我查看html源代码时,由于我动态创建了它,因此没有“Textbox1”。
Thanks. 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.