For example
搜索
How to get the value of the input text box and put it in the parameter of the a tag.
The premise is that in the U method of thinkphp
Reply content:
For example
搜索
How to get the value of the input text box and put it in the parameter of the a tag.
The premise is that in the U method of thinkphp
Processing of some data in the reference Onethink
1. Initialize each global JS variable first on the page
(function(){ var ThinkPHP = window.Think = { "ROOT" : "__ROOT__", //当前网站地址 "APP" : "__APP__", //当前项目地址 "PUBLIC" : "__PUBLIC__", //项目公共目录地址 "DEEP" : "{:C('URL_PATHINFO_DEPR')}", //PATHINFO分割符 "MODEL" : ["{:C('URL_MODEL')}", "{:C('URL_CASE_INSENSITIVE')}", "{:C('URL_HTML_SUFFIX')}"], "VAR" : ["{:C('VAR_MODULE')}", "{:C('VAR_CONTROLLER')}", "{:C('VAR_ACTION')}"] }})();
2. Encapsulation of various processing methodsThink.js
/* Extend thinkphp Object */(function ($) {/** *) get thinkphp Base configuration * @type {object} */var thinkphp = window. Think; /* Base Object Detection */thinkphp | | $.error ("thinkphp basic configuration not loaded properly! "); /** * Parse URL * @param {string} URL resolved URL * @return {object} parsed data */Thinkphp.parse_url = Functi On (URL) {var parse = Url.match (?:( [a-z]+): \/\/)? ([\w-]+ (?: \. [\w-]+) +)? (?::(\d+))? ([\w-\/]+)? (?:\? ((?:\ w+=[^#&=\/]*)? (?: &\w+=[^#&=\/]*) *))? (?: # ([\w-]+))? $/i); Parse | | $.error ("URL is not formatted correctly! "); return {"scheme": parse[1], "host": Parse[2], "port": parse[3], "Path": parse[4], "query": parse[5], "fragment": Parse[6]}; } THINKPHP.PARSE_STR = function (str) {var value = Str.split ("&"), VARs = {}, param; For (val in value) {param = value[val].split ("="); Vars[param[0]] = param[1]; } return VARs; } THinkphp.parse_name = function (name, type) {if (type) {/* Underline to Hump */Name.replace (/_ ([A-z])/g, f Unction ($, $) {return $1.touppercase (); }); /* Initial Capital Letter */name.replace (/[a-z]/, function ($) {return $0.touppercase (); }); } else {/* uppercase to lowercase */name = Name.replace (/[a-z]/g, function ($) {return "_" + $0.to lowercase (); }); /* Remove the underscore of the first character */if (0 = = = Name.indexof ("_")) {name = NAME.SUBSTR (1); }} return name; }//scheme://host:port/path?query#fragment thinkphp.u = function (URL, vars, suffix) {var info = This.parse_ur L (URL), Path = [], param = {}, reg; /* Verify info */Info.path | | $.error ("URL format error! "); url = info.path; /* Assembly URL */if (0 = = = Url.indexof ("/")) {//route mode this. Model[0] = = 0 && $.error ("The URL mode does not support the use of routing!" ("+ URL + ")"); /* Remove Right delimiter */if ("/" = = Url.substr ( -1)) {url = url.substr (0, Url.length-1)} url = ("/" = = this.) Deep)? URL.SUBSTR (1): Url.substr (1). replace (/\//g, this. Deep); url = "/" + URL; } else {//non-route mode/* resolves URL */path = Url.split ("/"); Path = [Path.pop (), Path.pop (), Path.pop ()].reverse (); PATH[1] | | $.error ("thinkphp.u (" + URL + ") does not specify a controller"); if (Path[0]) {param[this. Var[0]] = this. MODEL[1]? Path[0].tolowercase (): path[0]; } param[this. VAR[1]] = this. MODEL[1]? This.parse_name (Path[1]): path[1]; Param[this. VAR[2]] = Path[2].tolowercase (); url = "?" + $.param (param); }/* Parse parameter */if (typeof VARs = = = "string") {VARs = This.parse_str (VARs); } else if (!$.isplainobject (VARs)) {VARs = {}; }/* Parse URL's own parameters */Info.query && $.extend (VARs, This.parse_str (info.query)); if (vars) {URL + = "&" + $.param (VARs); } if (0! = this. Model[0]) {URL = url.replace ("?" + (Path[0]? this. Var[0]: this. VAR[1]) + "=", "/"). Replace ("&" + this. VAR[1] + "=", this. Deep). Replace (' & ' + this. VAR[2] + "=", this. Deep). Replace (/(\w+=&) | ( &?\w+=$)/g, ""). Replace (/[&=]/g, this. Deep); /* Add pseudo-static suffix */if (false!== suffix) {suffix = suffix | | this. Model[2].split ("|") [0]; if (suffix) {URL + = "." + suffix; }}} URL = this. APP + URL; return URL; }/* Set the value of the form */thinkphp.setvalue = function (name, value) {var first = Name.substr (0,1), input, i = 0, Val; if (value = = = "") return; if ("#" = = = First | | "." = = = First) {input = $ (name); } else {input = $ ("[name= '" + name + "']"); } if (Input.eq (0). Is (": Radio")) {//radio button Input.filter ("[value= '" + Value + "']"). each (function () {This.che cked = true}); } else if (Input.eq (0). Is (": checkbox")) {//check box if (!$.isarray (value)) {val = new Array (); Val[0] = value; } else {val = value; } for (i = 0, len = val.length; i < Len; i++) {Input.filter ("[value= '" + val[i] + "']"). each (f Unction () {this.checked = true}); }} else {//other form options directly set the value Input.val (value); }}) (JQuery);
3. Use
// 生成连接function editRule(id,status){ window.location.href = Think.U('Admin/AuthManager/editRule','id='+id+'&status='+status);}// 设置表单值,可以为ID、Class、NameThink.setValue('username','张三');
4. Can be downloaded oneThink
after the specific. Open it/Public/static/Think.js
Hope can help you, the above code is oneThink
encapsulated. I'm just a porter, and finally, in the page ThinkPHP
itself the U
method is unable to dynamically pass some JS variable
When PHP renders this page, the U method has generated a link
Please understand the generation process of a Web page
Page loading has resolved the U method, so the dynamic acquisition of the parameter is to add to the link, the way to get this with JS, and dynamic patchwork (above you all posted a variety of code I do not post), personally feel this is very cumbersome, look at you above the function, nothing but to do a search function, My implementation method is as follows:
A single JS function, event trigger to cobble together a tag URL, such as
var url = '{:u(xxxxx)}?key='+$("#keyword").val();
Can only use the way upstairs, in the JS file in the separate write method to control, because the template method is output as HTML before the production, so it can not be changed dynamically