我需要使用 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;
}
无论出于何种原因,尽管我在代码中的其他点使用了它来检查和设置属性,但我不断收到错误消息:
The property or method (elem.attr("class", "error");) is unsupported.
如何使用 jquery 将文本框的边框设置为红色?
最佳答案
elem
是原始 HTMLElement。您需要将其包装在 $(elem)
中,或者执行 elem.className = 'error'
关于jquery - 使用 jquery 将类添加到文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17845297/