【发布时间】:2014-07-26 11:58:30
【问题描述】:
当文本框为空时需要在class属性中添加“error”。
我有以下代码,但提交表单后此代码不显示。请指导我。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.0.min.js" ></script>
<script type="text/javascript">
$(document).ready(function(){
alert("page loaded");
$("#examle_frm_id input[type='submit']").click(function(){
alert("form has hitted");
if (jQuery("#fname").val() == ""){
$("#fname").addClass( "error" );
}
});
})
</script>
</head>
<body>
<form method="post" id="examle_frm_id" >
<input type="text" class="txt-firstname" name="fname" id="fname" />
<input type="submit" />
</form>
</body>
</html>
更多输入,我在“addClass”函数之后添加了 return false。我提交了表格。表单刚刚停止,我注意到在萤火虫中,添加了“错误”类。但是如果我删除了“return false”,页面会加载/刷新,我们在 jquery 中添加的内容都会从类中删除。
现在请指教。表单提交后如何根据条件动态添加某个类。
【问题讨论】:
标签: javascript jquery drupal