gpt4 book ai didi

javascript - Jquery unbind() 不适用于联系表单

转载 作者:行者123 更新时间:2023-12-01 03:51:21 26 4
gpt4 key购买 nike

我正在构建我的第一个联系表单。

在没有错误消息的情况下,我正在尝试解除绑定(bind)我的函数。
代码如下 - 无论出于何种原因,我不断在我的开发控制台中收到以下错误消息:

Uncaught TypeError: $(...).unbind is not a function

感谢任何帮助。

<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
</head>
<body>

<div class ='container'>
<h1>Get in touch!</h1>

<div id='error'></div>

<form>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter email">
</div>


<div class="form-group">
<label for="exampleInputPassword1">Subject</label>
<input type="password" class="form-control" id="subject" placeholder="Subject">
</div>


<div class="form-group">
<label for="exampleTextarea">Message</label>
<textarea class="form-control" id="message" rows="3"></textarea>
</div>
<button type="submit" class="btn btn-primary" id="submit">Submit</button>
</form>

</div>

<!-- jQuery first, then Tether, then Bootstrap JS. -->
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>

<script type='text/javascript'>
$('form').submit(function (e) {

e.preventDefault();
var error ="";

if ($('#subject').val() == "") {
error += 'The subject field is required.<br>';
}

if ($('#message').val() == "") {
error += 'The message field is required.<br>';
}


if ($('#email').val() == "") {
error += 'The email field is required.';
}

if (error != "" ) {
$("#error").html(
'<div class="alert alert-danger" role="alert"></p> <strong>Error(s) in your form:</strong></p>' + error + '</div>');
}else{
$("form").unbind('submit').submit();
}

});
</script>
</body>
</html>

最佳答案

您正在使用 jQuery 的 3.1.1.slim 版本。根据the jQuery site :

[the slim build] "...excludes ajax, effects, and currently deprecated code."

.unbind() method在 v3.0 中已弃用,因此不包含在您正在使用的版本中。

使用the .off() method相反。

关于javascript - Jquery unbind() 不适用于联系表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43163843/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com