我正在使用laravel 5.3。我正在尝试使用jquery validate插件验证表单数据。在数据验证之后,我想触发一个Ajax调用。这是我的代码
<!doctype html>
<html>
<head>
<meta name="_token" content="{!! csrf_token() !!}"/>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="/js/bootstrap-tagging.js"></script>
<link type="text/css" href="/css/bootstrap-tagging.css" rel="stylesheet">
<script src="/js/tapered.bundle.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.15.0/jquery.validate.js"></script>
<script src="/js/validation.js"></script>
<script>
$(document).ready(function() {
function ajaxCall() {
console.log("event occured");
$.ajax({
type: "POST",
url: '/storeproject',
data: { _token: "{{ csrf_token() }}" },
success: function( msg ) {
console.log(msg);
}
});
}
});
</script>
</head>
<body>
<form role="form" method="POST" action="javascript:ajaxCall()" id="test-form">
{!! csrf_field() !!}
<label>Project Name</label>
<input class="form-control" id="projectName" placeholder="Name" name="projectname" type="text" autofocus="">
<input type="submit" id="submit" name="submit" class="btn btn-primary" value="Add">
</form>
</body>
</html>
当我点击添加按钮时出现错误
VM681:1未捕获的ReferenceError:未定义ajaxCall at:1:1
但如果我避开文档就绪功能,它可以正常工作。有什么问题?
validation.js文件在这里
$(document).ready(function () {
$("#test-form").validate({
rules: {
projectname:{
required:true
}
}
});
});
答案 0 :(得分:2)
$(function() {
// Initialize form validation on the registration form.
// It has the name attribute "registration"
$("#test-form").validate({
rules: {
projectname:{
required:true
}
},
submitHandler: function(form) {
$.ajax({
type: "POST",
url: '/storeproject',
data: { _token: "{{"+$('#projectName').val()+"}}" },
success: function( msg ) {
console.log(msg);
}
});
}
});
});
&#13;
<!doctype html>
<html>
<head>
<meta name="_token" content="{!! csrf_token() !!}"/>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="/js/bootstrap-tagging.js"></script>
<link type="text/css" href="/css/bootstrap-tagging.css" rel="stylesheet">
<script src="/js/tapered.bundle.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.15.0/jquery.validate.js"></script>
</head>
<body>
<form role="form" method="POST" id="test-form">
{!! csrf_field() !!}
<label>Project Name</label>
<input class="form-control" id="projectName" placeholder="Name" name="projectname" type="text" autofocus="">
<input type="submit" id="submit" name="submit" class="btn btn-primary" value="Add">
</form>
</body>
</html>
&#13;