我试图创建一个带有表单验证的简单输入框,但我不能 100% 确定如果成功,如何将帖子发布到 URL。我不是 100% 确定我是否需要在 jquery 脚本中发布帖子,或者我是否还需要修改 views.py 文件中的任何内容......
<html>
<head>
<meta charset="utf-8">
<title>Demo</title>
</head>
<body>
<script src="{{ STATIC_URL }}js/jquery.js"></script>
<script type="text/javascript">
jQuery(function(){
$("#btn-search").click(function(){
$(".error").hide();
var hasError = false;
var searchReg = /^[a-zA-Z0-9-]+$/;
var searchVal = $("#search-text").val();
if(searchVal == '') {
$("#search-text").after('<span class="error">Please enter a search term.</span>');
hasError = true;
} else if(!searchReg.test(searchVal)) {
$("#search-text").after('<span class="error">Enter valid text.</span>');
hasError = true;
}
if(hasError == true) {return false;}
else {
$("#search-text").after('<span class="error">Search term accepted.</span>');
return false;
}
});
});
</script>
<form method="post" name="form1" action="/whatmask_output/">
{% csrf_token %}
<fieldset>
<div><label>Search:</label><input type="text" name="search-text" id="search-text" value="" size="32" /></div>
<div><label></label><input type="submit" value="Submit" id="btn-search" /></div>
</fieldset>
</form>
</body>
</html>
有任何想法吗 ??