I need to use a jquery function to add a css class I have created to textboxes on a form. This class will change the border of the textbox to red, to indicate that there is an error in the textbox and that the user cannot submit until the error is fixed. Here is my current code:
我需要使用jquery函数将我创建的css类添加到表单上的文本框中。此类将文本框的边框更改为红色,以指示文本框中存在错误,并且在修复错误之前用户无法提交。这是我目前的代码:
function validateRequired() {
var obj = $('.validate');
var message = "";
if (obj == null) {
return message;
}
obj.each(function (index, elem) {
if (elem.value == "") {
message += splitCamelCase(elem.id);
elem.attr("class", "error");
}
});
return message;
}
For whatever reason, though I've used this in other points in the code to check and set attributes, I keep getting an error saying:
无论出于何种原因,虽然我已经在代码中的其他点使用它来检查和设置属性,但我一直收到错误说:
The property or method (elem.attr("class", "error");) is unsupported.
属性或方法(elem.attr(“class”,“error”);)不受支持。
How can I set the border of the textbox to red using jquery?
如何使用jquery将文本框的边框设置为红色?
2 个解决方案
#1
3
elem
is the raw HTMLElement. You need to wrap it in $(elem)
, or do elem.className = 'error'
elem是原始的HTMLElement。你需要将它包装在$(elem)中,或者执行elem.className ='error'
#2
0
You could try to do instead of attribute:
您可以尝试而不是属性:
//...
message += splitCamelCase(elem.id);
$('this').addClass("error");
//...
#1
3
elem
is the raw HTMLElement. You need to wrap it in $(elem)
, or do elem.className = 'error'
elem是原始的HTMLElement。你需要将它包装在$(elem)中,或者执行elem.className ='error'
#2
0
You could try to do instead of attribute:
您可以尝试而不是属性:
//...
message += splitCamelCase(elem.id);
$('this').addClass("error");
//...