Пример:
Для недопущения отправки данных из существующей формы, до тех пор пока не установлена переменная:
"jQuery"
$("form").submit(function() {
if ($("input:first").val() == "correct") {
$("span").text("Validated...").show();
return true;
}
$("span").text("Not valid!").show().fadeOut(1000);
return false;
});
"HTML"
Type 'correct' to validate.
<form action="javascript:alert('success!');"> <div> <input type="text" /> <input type="submit" /> </div> </form> <span></span>
"CSS"
p { margin:0; color:blue; }
div,p { margin-left:10px; }
span { color:red; }
"Живой пример jQuery"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="http://slyweb.ru/css/jqueryiframe.css"
rel="stylesheet" type="text/css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("form").submit(function() {
if ($("input:first").val() == "correct") {
$("span").text("Validated...").show();
return true;
}
$("span").text("Not valid!").show().fadeOut(1000);
return false;
});
});
</script>
</head>
<body class="iframe">
Type 'correct' to validate.
<form action="javascript:alert('success!');">
<div>
<input type="text" />
<input type="submit" />
</div>
</form>
<span></span>
</body>
</html>
<style>
p { margin:0; color:blue; }
div,p { margin-left:10px; }
span { color:red; }
</style>
Пример:
Для недопущения отправки данных из существующей формы до тех пор пока не установлена переменная: