Skip to content

Instantly share code, notes, and snippets.

@ckentq
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ckentq/21724d11cb54c25fccca to your computer and use it in GitHub Desktop.
Save ckentq/21724d11cb54c25fccca to your computer and use it in GitHub Desktop.
(function() {
var checkDuplicate = function(elem, options){
};
$.fn.checkDuplicate = function(options){
var args = Array.prototype.slice.call(arguments);
return this.each(function() {
var currentTimer = 0;
var _code = '';
var $this = $(this);
var defaults = {
error_container : '#check_result',
waiting : '#waiting',
target_text : '編碼',
api_url: ''
}
this.options = $.extend(defaults, options);
// 檢查程序
var checkIt = function(){
if(_code == ''){
$(error_container).html(this.options.target_text+'不可為空白');
}else{
$(waiting).show();
$.post(this.options.api_url, { code: _code},
function(data){
$(this.options.waiting).hide();
if(data['result']) {
$(this.options.error_container).html(this.options.target_text+'可使用');
}else{
$(this.options.error_container).html(this.options.target_text+'已存在');
}
}, "json");
}
};
// 偵測
$this.on('keyup', function(){
_code = $this.val();
clearTimeout(currentTimer);
currentTimer = setTimeout("checkIt()", 800);
});
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment