Last active
August 29, 2015 14:04
-
-
Save ckentq/21724d11cb54c25fccca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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