简体   繁体   English

如何更新动态输入控件的背景颜色?

[英]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.

相关问题 如何使用“输入颜色和 onchange”更改 canvas 背景颜色 - How can i change canvas background color with “ input color and onchange ” 我如何更改选定的背景颜色输入 html/js - How i can change the selected background color input html/js 从输入中选择颜色后,如何使此按钮更改背景颜色? - How can I make this button change the background color after selecting color from the input? 如何使用颜色选择器更新 div 或任何元素的背景颜色 - How can I update the background color of my div or any element by using Color Picker 如何创建一个动态输入字段,该字段将根据输入中的 label 更新宽度 - How can I create a dynamic input field that will update width based on a label inside the input 如何让 Firefox 在 a:hover *before* javascript 例程运行时更新背景颜色? - How can I get Firefox to update background-color on a:hover *before* a javascript routine is run? 如何用按钮控制背景音乐? - How can I control background music with button? 如何更改滚动条上的背景颜色? - How can i Change background color on scroll? 当我输入并提交内容时,如何停止输入表单以更改背景颜色和文本颜色? - How do I stop the input form to change background color and text color when I input and submit something? 如何根据 onclick 更改输入字段背景颜色? - How do i change input field background color based on onclick?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM