Skip to content

Instantly share code, notes, and snippets.

@crazylion
Forked from ckentq/gist:21724d11cb54c25fccca
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazylion/5a976cd155c808b54caf to your computer and use it in GitHub Desktop.
Save crazylion/5a976cd155c808b54caf to your computer and use it in GitHub Desktop.
(function() {
var defaults = {
error_container : '#check_result',
waiting : '#waiting',
target_text : '編碼',
api_url: ''
}
var checkDuplicate = function(elem, options){
var currentTimer = 0;
var _code = '';
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);
});
};
$.fn.checkDuplicate = function(options){
var args = Array.prototype.slice.call(arguments);
return this.each(function() {
var $this = $(this);
var data = $this.data('checkDuplicate');
if (!data){
$this.data('checkDuplicate', (data = new cCeckDuplicate(this, options)));
}
if(typeof options == 'string'){
var argumentArray = args.slice(1);
data[options].apply(data,argumentArray);
}
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment