;(function($){ $.fn.jqsearch = function (options) { var defaults = { txtval: "输入关键词搜索!", keytxt1: "输入关键词搜索!", keytxt2: "输入的关键词字数不要过多!", keytxt3: "您输入的内容存在特殊字符!", keyid: "topkey", keyurl: "#", otherurl: "", otherparams: "", othertxtval: "请选中筛选或输入关键词搜索!", keyhref: "key", static: false }; var opts = $.extend(defaults, options); return this.each(function () { _this = $(this); if ($("#" + opts.keyid).val() == "") { $("#" + opts.keyid).val(opts.txtval); } $("#" + opts.keyid).focus(function () { _this.addclass("focus"); $(this).addclass("focus"); if ($(this).val() == opts.txtval) { $(this).val(""); } }); $("#" + opts.keyid).blur(function () { _this.removeclass("focus"); $(this).removeclass("focus"); if ($(this).val() == "") { $(this).val(opts.txtval); } }); function gosearchurl() { var searchinput = document.getelementbyid(opts.keyid); var isnull = true; //静态 var staticlinks = opts.keyurl; if (typeof (opts.otherparams) == "function") { var params = opts.otherparams(); for (var property in params) { isnull = false; if (opts.static) { staticurl += "_" + property + "_" + params[property]; } else { if (opts.otherurl != "") { opts.otherurl = appendurlparmfun(opts.otherurl, property, params[property]); } else { opts.keyurl = appendurlparmfun(opts.keyurl, property, params[property]); } } } } if (typeof (opts.otherparams) == "function" && isnull && (searchinput.value == "" || searchinput.value == opts.txtval)) { alert(opts.othertxtval); return false; } if (!isnull && opts.otherurl != "") { window.location = opts.otherurl; return false; } if (isnull && (searchinput.value == "" || searchinput.value == opts.txtval)) { alert(opts.keytxt1); searchinput.focus(); return false; } if (isnull && (searchinput.value == "" || searchinput.value == opts.txtval)) { alert(opts.keytxt1); searchinput.focus(); return false; } if (searchinput.value.length > 50) { alert(opts.keytxt2); searchinput.focus(); return false; } var pattern = new regexp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~!@#¥……&*()—|{}【】‘;:”“'。,、?]"); if (pattern.test(searchinput.value)) { alert(opts.keytxt3); searchinput.focus(); return false; } if (!(searchinput.value == "" || searchinput.value == opts.txtval)) { if (opts.static) { //静态 staticlinks = opts.keyurl; staticurl = "_" + opts.keyhref + "_" + searchinput.value + ".html" staticlinks = staticlinks.replace(".html", staticurl); window.location = staticlinks; } else { //动态 if (opts.keyurl.indexof("?") == -1) { window.location = opts.keyurl + "?" + opts.keyhref + "=" + escape(searchinput.value); } else { window.location = opts.keyurl + "&" + opts.keyhref + "=" + escape(searchinput.value); } } } else { if (opts.static) { window.location = staticlinks; } else { window.location = opts.keyurl; } } return true; } function entersearch() { var event = window.event || arguments.callee.caller.arguments[0]; if (event.keycode == 13) { gosearchurl(); } } function checkcomments() { var event = window.event || arguments.callee.caller.arguments[0]; if ((event.keycode > 32 && event.keycode < 48) || (event.keycode > 57 && event.keycode < 65) || (event.keycode > 90 && event.keycode < 97)) { event.returnvalue = false; } } function stripscript(s) { var pattern = new regexp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~!@#¥……&*()——|{}【】‘;:”“'。,、?]"); var rs = ""; for (var i = 0; i < s.length; i++) { rs = rs + s.substr(i, 1).replace(pattern, ''); } return rs; } function appendurlparmfun(url, key, value) { var arrayparams = url.split("?"); var result = arrayparams[0] + "?"; if (url.indexof("?") > 0) { var parms = arrayparams[1].split("&"); var exsit = false; for (var i = 0; i < parms.length; i++) { var param = parms[i].split("="); if (param[0] == key) { exsit = true; result += param[0] + "=" + value + "&"; //修改参数 } else { result += param[0] + "=" + param[1] + "&"; //保持原样 } } if (exsit == false) { result += key + "=" + value + "&"; //不存在时,附加参数 } } else { result += key + "=" + value; } result = result.substring(result.length - 1, result.length) == "&" ? result.substring(0, result.length - 1) : result; return result; } _this.click(function () { gosearchurl() }); $("#" + opts.keyid).keydown(function () { entersearch() }); }) } })(jquery);