愁人,編程語言這個東西,學完了能經常使用還好點,要是不經常使用的話,過不了幾天就忘記用法了,這不昨天在本身練習用django學工具的時候,居然忘記了jquery中done的使用方法,可是卻又很清楚的記得本身以前是用過的,就是想不起來,你說愁人不,寫篇博客記錄下吧。javascript
如下樣例是在YII框架下寫的,在CSRF_TOKEN的地方各類框架都不盡相同:css
<script type="text/javascript">html
$(document).ready(function(){java
$("#add").click(function(){jquery
var start = $("#start").val();django
var end = $("#end").val();編程
var CSRF_TOKEN = "<?= Yii::$app->getRequest()->getCsrfToken(); ?>";json
var re = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;app
var res = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;框架
if (!re.test(start)){
$("#status").html(" * ip地址格式不正確");
}else if (!res.test(end)){
$("#status").html(" * 輸入不正確,請輸入1-255之間的數字");
}else{
$.post('/hostmanager/addsystem/',
{
start:start,
end:end,
_csrf: CSRF_TOKEN
},null,'json').done(function(response){
if (response.status == 'RESULTS'){
var message= "<h3>" + response.message + "<h3>";
alertify.alert(message);
$(function() {
window.setTimeout(function(){
location.href="/hostmanager/";
}, 5000);
});
}else if (response.status == 'TESTCONN'){
var testconn = response.message;
document.write(testconn);
if(testconn == 1){
$('.exam').sibling('.light').css('background','green');
}else {
$('.exam').sibling('.light').css('background','red');
}
}else {
$("#status").html(" ");
}
});
};
});
});
</script>