1
我試圖創建一個簡單的輸入框與表單驗證,但即時通訊不是100%確定如果成功發佈一個帖子的URL。我不是100%肯定,如果我只是需要內的jQuery腳本,或者如果我還需要在views.py文件中修改任何發出帖子...Django/Jquery表單提交
<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>
任何想法?