Jump to content
  • 0

Непонятная ошибка


Cerberus
 Share

Question

Добрый день.

Прошу извинить, возможно вопрос совсем уж пустячный, но всё же.

JavaScript только начинаю изучать, смотрю видеоурок и по нему всё повторяю.

Вот такой HTML

<script type="text/javascript" src="script.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<title>Test JavaScript</title>
</head>
<body>
<form name="form" method="post">
<input type="text" name="name" class="name" size="30">
<div class="errorName"></div>
<input type="button" value="Click" onClick="return validate(form)">
</form>

CSS

.errorList{
background-color:#999;
border:2px solid red;
}

JS

function validate(form){
var name = form.name.value;

if(name == ""){
$(".errorName").html("<b>Поле имя не заполнено</b>").css({"color":"red"});
$(".name").toggleClass("errorList");
$(".name").click(function(){
$(".name").removeClass("errorList");
$(".errorName").html("");
});
return false;
}

}

И вот такое мне выдает в консоли при незаполненном поле после нажатия на кнопку

[21.07.2012 14:54:56] JavaScript - file://localhost/Z:/home/php/www/valid/test.html
Event thread: click
Uncaught exception: ReferenceError: Undefined variable: $
Error thrown at line 6, column 2 in validate(form) in file://localhost/Z:/home/php/www/valid/script.js:
$(".errorName").html("<b>Поле имя не заполнено</b>").css({"color":"red"});
called from line 1, column 0 in <anonymous function>(event) in file://localhost/Z:/home/php/www/valid/test.html:
return validate(form)

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Переписал ваш код)

Работающая версия:

HTML:


<form name="form" method="post" id='form'>
<input type="text" name="name" class="name" size="30">
<div class="errorName"></div>
<input type="button" value="Click" id='click'>
</form>

JS:


$('#click').click(function() {
alert(validate($('#form input[name="name"]')))
});

function validate(inp) {
if (!inp.val()) {
$(".errorName").html("<b>Поле имя не заполнено</b>").css({
"color": "red"
});
$(".name").toggleClass("errorList");
$(".name").click(function() {
$(".name").removeClass("errorList");
$(".errorName").html("");
});
return false;
}

return true;
}

CSS:


.errorList{
background-color:#999;
border:2px solid red;
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. See more about our Guidelines and Privacy Policy