JQUERY-based multi-level linkage code

Source: Internet
Author: User

Jquery. select. more. js
Copy codeThe Code is as follows:
(Function ($ ){
$. Fn. doselectmore = function (settings ){
Var dfop = {
Namekey: "name ",
Pnamekey: "name ",
Idkey: "id ",
Selectname: "sel ",
Method: "POST ",
Datatype: "json ",
Param :{},
Pval: null,
Chckval: null,
Chckvalarry: null,
Pname: false,
Nname: false,
Vl: 0,
Url: false,
Data: false
};
$. Extend (dfop, settings );
// Alert (eobj (dfop ));
Var me = $ (this );
If (! Dfop. nname ){
Dfop. nname = (dfop. selectname + (dfop. vl + 1 ));
}
If ((! Dfop. pname) & dfop. vl> 0 ){
Dfop. pname = dfop. selectname + (dfop. vl-1 );
}
If (! Dfop. data ){
If (dfop. url ){
Var param = {};
$. Ajax ({
Type: dfop. method,
Url: dfop. url,
Data: dfop. param,
DataType: dfop. datatype,
Success: function (data ){
Dfop. data = data;
Selectmorebuilder (me, dfop );
},
Error :( function (request, status, err ){
Var errText = request. responseText;
Var ErrMessage = "" + request. status + "error message, \ n ";
ErrMessage + = "error message:" + request. statusText + "\ n" + errText. substring (errText. indexOf ("<pre>") + 5, errText. indexOf ("</pre> "));
Alert (ErrMessage );
})
});
}
} Else {
Selectmorebuilder (me, dfop );
}
Function selectmorebuilder (thisme, df ){
If (df. chckval! = Null & df. chckvalarry = null ){
Var pid = df. chckval;
Var k = 1;
Df. chckvalarry = new Array ();
Df. chckvalarry. push (pid );
While (k> 0 ){
K = 0;
$. Each (df. data, function (I, item ){
If (item [df. idkey] = pid & item [df. pnamekey]! = Df. pval ){
Pid = item [df. pnamekey];
Df. chckvalarry. unshift (pid );
K ++;
}
});
}
}
// Alert (eobj (df. chckvalarry ));
Var select =$ ("<select> </select> ");
Select. attr ({
Name: df. selectname + dfop. vl,
Id: df. selectname + dfop. vl,
Nname: df. nname,
Pname: df. pname,
Vl: df. vl
});
Var sdiv = null;
If (dfop. vl = 0 ){
Sdiv =$ ("<div> </div> ");
Thisme. after (sdiv). remove ();
Sdiv. append ("<input type = \" hidden \ "name = \" "+ df. selectname +" \ "> ");
Sdiv. append (select );
Sdiv. get (0). t = df;
} Else {
Thisme. removeselectmore ();
Sdiv = $ ("input [name =" + df. selectname + "]"). parent ();
Sdiv. append (select );
}
Select. empty ();
Var counti = 0;
$. Each (df. data, function (I, item ){
If (item [df. pnamekey] = df. pval ){
Select. append ("<option value = '" + item [df. idkey] + "'>" + item [df. namekey] + "</option> ");
Counti ++;
}
});
If (counti = 0 ){
Select. remove ();
}
Else {
Select. change (function (){
Var nselect = $ ("#" + $ (this). attr ("nname "));
If (nselect. length = 0 ){
Nselect = $ ("<select> </select> ");
Sdiv. append (nselect );
}
Nselect. doselectmore ({
Namekey: df. namekey,
Pnamekey: df. pnamekey,
Idkey: df. idkey,
Selectname: df. selectname,
Param: df. param,
Pval: $ (this). val (),
Vl: df. vl + 1,
Chckvalarry: sdiv. get (0). t. chckvalarry,
Data: df. data
});
});
If (df. chckvalarry! = Null ){
If (df. chckvalarry. length> = df. vl)
Select. val (df. chckvalarry [df. vl]);
}
If (df. vl = 1 & df. chckvalarry! = Null & df. chckvalarry [1]! = Select. val ()){
Var ddf = sdiv. get (0). t;
Ddf. chckvalarry = null;
Ddf. chckval = null;
Sdiv. get (0). t = ddf;
}
Select. change ();
$ ("Input [type = hidden] [name =" + df. selectname + "]"). val ($ ("input [type = hidden] [name =" + df. selectname + "]"). getselectmoreval ());
}
}
};
$. Fn. getselectmoreval = function (){
Var me = $ (this );
If (me. size () = 0) return;
Var sdiv = me. parent ();
If (sdiv. size () = 0) return;
Var df = sdiv. get (0). t;
Var nselect = $ ("#" + df. selectname + df. vl );
Var v = null;
While (nselect. size ()> 0 ){
V = nselect. val ();
Nselect = $ ("#" + nselect. attr ("nname "));
}
Return v;
};
$. Fn. setselectmoreval = function (idv ){
Var me = $ (this );
If (me. size () = 0) return;
Var sdiv = me. parent ();
If (sdiv. size () = 0) return;
Var df = sdiv. get (0). t;
Df. chckval = idv;
If (idv! = Null ){
Var pid = df. chckval;
Var k = 1;
Df. chckvalarry = new Array ();
Df. chckvalarry. push (pid );
While (k> 0 ){
K = 0;
$. Each (df. data, function (I, item ){
If (item [df. idkey] = pid & item [df. pnamekey]! = Df. pval ){
Pid = item [df. pnamekey];
Df. chckvalarry. unshift (pid );
K ++;
}
});
}
}
Sdiv. get (0). t = df;
Var nselect = $ ("#" + df. selectname + df. vl );
Nselect. val (df. chckvalarry [0]);
Nselect. change ();
};
$. Fn. removeselectmore = function (){
If ($ (this). attr ("nname ")! = Null ){
$ ("#" + $ (This). attr ("nname"). removeselectmore ();
}
$ (This). remove ();
};
}) (JQuery );

Help.html
Copy codeThe Code is as follows:
Success <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Script src = ".../../common. js" type = "text/javascript"> </script>
<Script src = "..././jquery/jquery-1.3.2.min.js" type = "text/javascript"/> </script>
<Link type = "text/css" rel = "stylesheet" href = "../SyntaxHighlighter/Styles/SyntaxHighlighter.css"> </link>
<Script language = "javascript" src = "../SyntaxHighlighter/Scripts/shCore. js"> </script>
<Script language = "javascript" src = "../SyntaxHighlighter/Scripts/shBrushJScript. js"> </script>
<Script language = "javascript" src = "../SyntaxHighlighter/Scripts/shBrushXml. js"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
Dp. SyntaxHighlighter. ClipboardSwf = '../SyntaxHighlighter/Scripts/clipboard.swf ';
Dp. SyntaxHighlighter. HighlightAll ('code ');
});
</Script>
<Title> Insert title here </title>
</Head>
<Body>
<B> Method Name: </B> doselectmore <br/>
<B> purpose: </B> instantiate an object as a multi-choice box. <br/>
<B> example: </B> html
<Pre name = "code" class = "html">
<! -- Head -->
<Script src = "jquery. select. more. js" type = "text/javascript"/> </script>
<! -- Body -->
<Input type = "text" name = "illegbasinfo">
</Pre>
Js:
<Pre name = "code" class = "js">
/**
※This parameter is required,★Select either ,? Is not required
※Namekey: name of the Data Type display,
※Pnamekey: the name of the upper-level primary key of the data,
※Idkey: the name of the Data primary key,
※Selectname: name of the Selection box,
※Pval: the value of the first-level upper-level node,
? Chckval: selected by default,
★Url: false,
? Param: parameters are input when a url is used,
★Data: false
**/
$ ("Input [name = illegbasinfo]"). doselectmore ({
Url: '../.../abc/dic/illbasinfo/loadall. do ',
Pval: 0,
Namekey: "illegbasinfo ",
Pnamekey: "parillegbasid ",
Idkey: "illegbasinfoid ",
Selectname: "illegbasinfo"
});
</Pre>
<B> Method Name: </B> doselectmore <br/>
<B> purpose: </B> obtain the selected object value. <br/>
<B> example: </B> js:
<Pre name = "code" class = "js">
$ ("Input [name = test1]"). click (function (){
Alert ($ ("input [name = illegbasinfo]"). getselectmoreval ());
});
</Pre>
<B> Method Name: </B> setselectmoreval (val) <br/>
<B> purpose: </B> set a value for the multiple choice box. <br/>
<B> example: </B> js:
<Pre name = "code" class = "js">
$ ("Input [name = test2]"). click (function (){
$ ("Input [name = illegbasinfo]"). setselectmoreval (347 );
});
</Pre>
</Body>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.